随笔分类 -  TypeScript

将某个类型断言为另一个与之毫无关系的类型
摘要:rule as any as Foo // 双重断言 阅读全文

posted @ 2021-09-10 10:10 aisowe 阅读(19) 评论(0) 推荐(0) 编辑

将 ts 代码转成 js 代码
摘要:index.ts function foo(bar: string, baz: number): (string | number)[] { return [bar, baz]; } bash tsc index.ts 阅读全文

posted @ 2021-09-10 10:08 aisowe 阅读(2267) 评论(0) 推荐(0) 编辑

即时编译、运行 typescript 代码
摘要:bash npm i ts-node -g npm i nodemon -g index.ts const foo: string = 'Hello' console.log(foo) bash nodemon index.ts 阅读全文

posted @ 2021-09-10 09:58 aisowe 阅读(118) 评论(0) 推荐(0) 编辑

访问联合类型中某个类型特有的属性或方法
摘要:index.ts interface Cat { name: string run(): void } interface Fish { name: string swim(): void } function isCat(animal: Cat | Fish): boolean { return 阅读全文

posted @ 2021-09-10 09:36 aisowe 阅读(112) 评论(0) 推荐(0) 编辑

TS 定义一个最简单的字符串数组类型
摘要:index.ts const foo: string[] = ["foo", "bar"] 阅读全文

posted @ 2021-09-09 10:52 aisowe 阅读(2087) 评论(0) 推荐(0) 编辑

TS 定义一个最简单的元组
摘要:index.ts const lilei: [string, number] = ["Lilei", 23] 阅读全文

posted @ 2021-09-09 10:51 aisowe 阅读(243) 评论(0) 推荐(0) 编辑

TS 定义一个抽象类(不允许被实例化)
摘要:index.ts abstract class Foo { constructor() {} className = "Foo"; } class FooChild extends Foo { constructor() { super(); } } // const foo = new Foo() 阅读全文

posted @ 2021-09-09 10:51 aisowe 阅读(193) 评论(0) 推荐(0) 编辑

TS 定义数值类型
摘要:index.ts let decLiteral: number = 6 // 10 let hexLiteral: number = 0xf00d // 16 let binaryLiteral: number = 0b1010 // 2 let octalLiteral: number = 0o7 阅读全文

posted @ 2021-09-09 10:50 aisowe 阅读(690) 评论(0) 推荐(0) 编辑

TS 定义函数的可选参数类型
摘要:index.ts function foo(name: string, age?: number) { console.log(name + age); } foo("a"); // "aundefined" 阅读全文

posted @ 2021-09-09 10:49 aisowe 阅读(4247) 评论(0) 推荐(0) 编辑

TS 定义接口的可选属性
摘要:index.ts interface IPerson { name: string age: number gender?: number } const lilei: IPerson = { name: "Lilei", age: 23, } 阅读全文

posted @ 2021-09-09 10:49 aisowe 阅读(1018) 评论(0) 推荐(0) 编辑

TS 定义接口的任意一个属性
摘要:index.ts interface IPerson { name: string age: number family?: any[] // Error,因为不是任意类型的子集 [propName: string]: string | number // 一般设置 any,因为其他类型必需是任意类 阅读全文

posted @ 2021-09-09 10:49 aisowe 阅读(2571) 评论(0) 推荐(0) 编辑

TS 定义函参剩余参数
摘要:index.ts function foo(...rest: any[]) { console.log(rest) } foo(1, 2, 3) // [1, 2, 3] 阅读全文

posted @ 2021-09-09 10:48 aisowe 阅读(290) 评论(0) 推荐(0) 编辑

处理因使用 BigInt 等最新语法时 ts 编译报错
摘要:tsconfig.json { "compilerOptions": { "target": "ESNEXT", "lib": ["DOM", "ESNEXT"], }, } index.ts const a = 11n 阅读全文

posted @ 2021-09-09 10:32 aisowe 阅读(274) 评论(0) 推荐(0) 编辑

TS 查找第三方声明文件
摘要:browser https://www.typescriptlang.org/dt/search?search= 阅读全文

posted @ 2021-09-09 10:31 aisowe 阅读(141) 评论(0) 推荐(0) 编辑

ts 中表现元组越界后的行为
摘要:index.ts const lilei: [string, number] = ["Lilei", 23]; lilei.push(NaN); // 3 lilei.push("NaN"); // NaN lilei.push(true); // string | number -> Error 阅读全文

posted @ 2021-09-09 10:25 aisowe 阅读(195) 评论(0) 推荐(0) 编辑

TypeScript class 便捷设置实例属性
摘要:index.ts class Foo { constructor(public name: string, public readonly age: number) {} } const foo = new Foo("foo", 23); console.log(foo.name); console 阅读全文

posted @ 2021-09-09 10:24 aisowe 阅读(157) 评论(0) 推荐(0) 编辑

编写一个基本通用的 tsconfig.json 配置文件
摘要:tsconfig.json { "compilerOptions": { "target": "ESNEXT", "lib": ["DOM", "ESNEXT"], "module": "commonjs", "sourceMap": true, "outDir": "./dist", "modul 阅读全文

posted @ 2021-09-09 10:23 aisowe 阅读(103) 评论(0) 推荐(0) 编辑

怎样安装并编译TypeScript?
摘要:1. 使用: npm -v 查看是否安装了 npm , 如果没有安装, 请前往 Nodejs 官网 下载安装, 下图表示已经安装 npm , 版本为: 6.9.0 . 2. 使用 tsc -v 查看是否安装了 typescript , 下面表示已经安装, 版本为: 3.5.3, 如果没有安装, 可以 阅读全文

posted @ 2019-08-26 23:26 aisowe 阅读(3393) 评论(0) 推荐(0) 编辑

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示