[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 ...
随机推荐
- cookie和seesion区别
cookie 和session 的区别详解 这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie ...
- Ambari?自动部署Hadoop集群
自动部署?Ambari Ambari 跟 Hadoop 等开源软件一样,也是 Apache Software Foundation 中的一个项目,并且是顶级项目.就 Ambari 的作用来说,就是创建 ...
- drupal 8——在CKEditor中导入video media时添加caption会导致video缩小至消失
在CKEditor中,我点击media browser,选择video型的media,并在caption中输入video的名字.当我保存后发现在前台页面的video消失了,只留下video的名字,点击 ...
- VC socket api使用引入
1.在创建项目时勾上windows socket api的使用 2.头文件 #pragma comment(lib,"WS2_32.lib") 3.初始化 WSADATA dat ...
- centos7下手动制作trove镜像
获取镜像 [root@bldattet1 ~]# wget http://mirrors.aliyun.com/centos/7.5.1804/isos/x86_64/CentOS-7-x86_64 ...
- JS高级——变量提升
JS执行过程 1.首先是预解析:预解析过程最重要的是提升,在JavaScript代码在预解析阶段,会对以var声明的变量名,和function开头的语句块,进行提升操作 2.执行操作 全局中解析和执行 ...
- jQuery——切换toggle
toggle()-----显示隐藏 toggleClass-------添加类或者删除类 <!DOCTYPE html> <html lang="en"> ...
- 6、scala Map和Tuple
1. 创建Map 2.访问Map元素 3.修改Map元素的值 4.遍历Map 5.SortedMap和LinkedHashMap 6.Map的元素类型Tuple 1. 创建Map 创建不可变的Ma ...
- CAD取Excel表格(com接口)
1 2 3 4 5 6 7 8 9 10 11 12 MxDrawResbuf ret = (MxDrawResbuf)axMxDrawX1.Call("ExApp_GetExcel&quo ...
- SpringMVC注解配置处理器映射器和处理器适配器
一.springmvc.xml中配置方式 <!--注解映射器 --> <bean class="org.springframework.web.servlet.mvc.me ...