[Tools] VS Code Tips
2024-09-27 11:24:18
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.
最新文章
- 使用ndk编译c可执行程序
- java工厂-积木系列
- Linux下使用Eclipse开发Hadoop应用程序
- c/c++中#和##链接符号的用法
- webapp开发要点记录
- NodeJS优缺点及适用场景讨论
- Eclipse中为自己写完的函数添加注释(快捷键ALT+SHIFT+J)
- 08 Transactions
- 使用spring的jdbcTemplate-----用JDBC模板查询数据库
- 测试通过Word直接发布博文
- Swift入门教程:基本语法大全
- LanSoEditor_common ---android平台的视频编辑SDK
- Java并发编程:volatile 关键字
- Pyhton爬虫实战 - 抓取BOSS直聘职位描述 和 数据清洗
- javascript类型判断方法
- java的hashmap与hashtable说明,简单易理解
- ZKW线段树入门
- 基于ubuntu的docker安装
- oracle 11g完美卸载
- Linux内核源代码分析方法
热门文章
- Java的Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区别???
- Java类加载机制总结
- Javascript实现导航锚点滚动效果实例
- CSS——img
- CSS——padding
- windows下使用批处理设置环境变量
- GatewayWorker + LayIM实现即时聊天
- Web前端性能优化——提高页面加载速度
- Git学习总结二(版本回退)
- matplotlib命令与格式:标题(title),标注(annotate),文字说明(text)