[Tools] VS Code Tips

Inside one file, you can freely mark the number 1-9:
shift + cmd + [-]
And jump to Number of bookmark:
cmd + [-]


It helps to generate type safe interface based the json file you provided.
For example you can create a json file called pokemon.json:
{
"id": ,
"name": "Bulbasaur",
"img": "http://www.serebii.net/pokemongo/pokemon/001.png",
"type": [ "Grass", "Poison" ],
"weaknesses": [ "Fire", "Ice", "Flying", "Psychic" ]
}
Then in the file you want to generate the code, open vs cammand panel, enter "Paste Json...", enter "Pokemon"...
It will genearte type safe code and lots of utitlties code.
export interface Pokemon {
id: number;
name: string;
img: string;
type: string[];
weaknesses: string[];
}
// Converts JSON strings to/from your types
// and asserts the results of JSON.parse at runtime
export namespace Convert {
export function toPokemon(json: string): Pokemon {
return cast(JSON.parse(json), r("Pokemon"));
}
export function pokemonToJson(value: Pokemon): string {
return JSON.stringify(value, null, 2);
}
function cast<T>(obj: any, typ: any): T {
if (!isValid(typ, obj)) {
throw Error(`Invalid value`);
}
return obj;
}
function isValid(typ: any, val: any): boolean {
if (typ === "any") { return true; }
if (typ === null) { return val === null; }
if (typ === false) { return false; }
while (typeof typ === "object" && typ.ref !== undefined) {
typ = typeMap[typ.ref];
}
if (Array.isArray(typ)) { return isValidEnum(typ, val); }
if (typeof typ === "object") {
return typ.hasOwnProperty("unionMembers") ? isValidUnion(typ.unionMembers, val)
: typ.hasOwnProperty("arrayItems") ? isValidArray(typ.arrayItems, val)
: typ.hasOwnProperty("props") ? isValidObject(typ.props, typ.additional, val)
: false;
}
return isValidPrimitive(typ, val);
}
function isValidPrimitive(typ: string, val: any) {
return typeof typ === typeof val;
}
function isValidUnion(typs: any[], val: any): boolean {
// val must validate against one typ in typs
return typs.some((typ) => isValid(typ, val));
}
function isValidEnum(cases: string[], val: any): boolean {
return cases.indexOf(val) !== -1;
}
function isValidArray(typ: any, val: any): boolean {
// val must be an array with no invalid elements
return Array.isArray(val) && val.every((element) => {
return isValid(typ, element);
});
}
function isValidObject(props: { [k: string]: any }, additional: any, val: any): boolean {
if (val === null || typeof val !== "object" || Array.isArray(val)) {
return false;
}
return Object.getOwnPropertyNames(val).every((key) => {
const prop = val[key];
if (Object.prototype.hasOwnProperty.call(props, key)) {
return isValid(props[key], prop);
}
return isValid(additional, prop);
});
}
function a(typ: any) {
return { arrayItems: typ };
}
function u(...typs: any[]) {
return { unionMembers: typs };
}
function o(props: { [k: string]: any }, additional: any) {
return { props, additional };
}
function m(additional: any) {
return { props: {}, additional };
}
function r(name: string) {
return { ref: name };
}
const typeMap: any = {
"Pokemon": o({
id: 0,
name: "",
img: "",
type: a(""),
weaknesses: a(""),
}, false),
};
}

A easy way to dealing with Git.


Good for demoing the code in a team.


[Tools] VS Code Tips的更多相关文章
- Visual Studio Code Tips
新项目要用到Visual Studio Code, 在使用的过程中有些tips, 记录下来以便查阅. 1. 自动保存代码 文件 => 自动保存 2. 帮助输入代码模式 扩展 => 安装HT ...
- SQL Server Code tips (持续更新)
1. 表存在,查询语句也能执行,但是表名下面总是有条红线,说对象名无效 CTRL + SHIFT +R 刷新本地缓存就可以了 2. IDE (Integrated Development Envi ...
- [notes] some code tips
genericizing-codehtml, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin ...
- maven 编译出现初始化异常:com/sun/tools/javac/code/TypeTags
使用的式jdk11 lombok式1.16.4 错误原因:版本不匹配 升级lombok到1.18.4 问题解决
- php之code tips
使用list来实现一次获取explode后的特定段值: list( , $mid) = explode(';', $string); 使用NULL === 来代替is_null: is_null和 N ...
- android xmlns:tools用法
一开始不明白,后来删掉这个属性之后发现会出现一个提示: pick preview layout from the "Fragment Layout" context menu 原来 ...
- Android code wiki
Android code wiki Tip1: 类的全局静态变量的使用,这样可以静态变量只分配一次内存,可以不通过类的对象也就是可以通过类名直接使用该变量.(使用场景:Request_Code ,Re ...
- Android 之 tools:context和tools:ignore两个属性的作用
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- visual code golang配置
前言 其实环境搭建没什么难的,但是遇到一些问题,主要是有些网站资源访问不了(如:golang.org), 导致一些包无法安装,最终会导致环境搭建失败,跟据这个教程几步,我们将可以快速的构建golang ...
随机推荐
- Codeforces 903G 巧妙的线段树
思路: 巧妙的线段树 想方法将网络流往数据结构方向转化 http://www.cnblogs.com/yyf0309/p/8724558.html //By SiriusRen #include &l ...
- mysql多表查询 查询排序
有 ask 问题表 和 answer回答表 回答表中的ask_id和 ask表中的id对应 1.查询 /*查询回答了的 */select a.id,a.title,count(b.ask_id) ...
- SRM-697-DIV2
Div2 Medium: DivisibleSetDiv2 Problem Statement You are given a vector <int> b containing ...
- Offer收割_4
1.水题 2.BFS宽搜(使用优先队列priority_queue) 4.题意:给数组a.要求重排列数组,使得数组中的任意相邻的两个元素不同.如果存在多个方案,那么选择字典序最小的方案. 如果不能满 ...
- UE4 集成讯飞听写插件
搞了几天,有些坑记录一下. 3个方面的知识需要学习 1.制作UE4插件 2.引入第三方库 3.讯飞听写的api 一看是参考 https://blog.csdn.net/u012793104/artic ...
- JSP中如何利用JS实现登录页面的跳转(JSP中如何利用JS实现跳转页面)
<%! <% url = word = } ...
- 【译】x86程序员手册05 - 2.3寄存器
2.3 Registers 寄存器 The 80386 contains a total of sixteen registers that are of interest to the applic ...
- php用户注册常用检测、写入
// 判断数据库是否已经存在 $check_sql = "select * from user where idNumber='$idNumber'"; $check_query ...
- Cuder - 用C++11封装的CUDA类
以前写cuda:初始化环境,申请显存,初始化显存,launch kernel,拷贝数据,释放显存.一个页面大部分都是这些繁杂但又必须的操作,有时还会忘掉释放部分显存. 今天用C++11封装了这些CUD ...
- maxtrid 3D视差
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...