随笔分类 - ts
typescript
-
【TypeScript】十四、泛型
摘要:interface Add { } function add<T>(a: T, b: T): T[] { return [a, b] } let res = add<number>(1, 2) console.log(res) 阅读全文
-
【TypeScript】十三、迭代器,生成器
摘要:迭代器 let s = [1,2,3,4] let it:Iterator<number> = s[Symbol.iterator]() console.log(it.next()); console.log(it.next()); console.log(it.next()); console.l 阅读全文
-
【TypeScript】十二、Symbol
摘要:let s: symbol = Symbol("123") let s1: symbol = Symbol("123") let o = { [s]: "111" } console.log(o[s]); console.log(s, s1, s1 s); 阅读全文
-
【TypeScript】十一、never类型
摘要:let a: string & number function error(message: string): never { throw new Error(message) } // error("my error") function loop(): never { while(true) { 阅读全文
-
【TypeScript】十、类型推断|类型别名
摘要:// 类型推断 let a = "13" // 类型别名 type s = string type sn = string|number type cb = ()=>string type T = 'off' | 'on' 阅读全文
-
【TypeScript】九、枚举类型
摘要:数字枚举 enum A { red, green, blue, } console.log(A.red); console.log(A.green); console.log(A.blue); 增长枚举 enum A { red=1, green, blue, } console.log(A.red 阅读全文
-
【TypeScript】八、元组
摘要:// 元组, 需要严格按照元组声明的类型给值 let arr: [string, number] = ['1', 1] console.log(arr) console.log(arr[0]); let excel:[number, string][] = [ [0, '白云区'] ] 阅读全文
-
【TypeScript】七、抽象类
摘要:抽象类被继承使用,抽象类中abstract修饰的方法不能有实现,实现抽象类的类需要实现抽象类中所有抽象方法 abstract class A { name: string constructor(name: string) { this.name = name } abstract run(time 阅读全文
-
【TypeScript】六、Class
摘要:类定义 class Person { private name: string age: number constructor(name: string, age: number) { this.name = name this.age = age } display(): void { conso 阅读全文
-
【TypeScript】五、类型断言|联合类型|交叉类型
摘要:// 类型断言 interface A { name: string } interface B { age: string } let f1 = function(type: A|B): void { let x = (<A>type).name let y = (type as A).name 阅读全文
-
【TypeScript】四、数组与函数重载
摘要:数组 let arr: number[] = [1, 2, 3, 4,] let arr1: string[] = ["hello", "world"] let arr2: boolean[] = [true, false] let arr3: any[] = [1, '1', true] let 阅读全文
-
【TypeScript】三、接口和对象
摘要:接口 interface t1{ name:string } let obj:t1 = { name: "aoaoao" } console.log(obj); // 名字一样的接口会合并 interface t1{ name:string } interface t1 { age:number } 阅读全文
-
【TypeScript】二、ts运行工具和任意类型
摘要:编译工具 yarn global add ts-node yarn global add @types/node any let str: any = '123456' str = {} console.log(str) str = 123 console.log(str) unknow let s 阅读全文
-
【TypeScript】一、基础类型
摘要:字符串 let str:string = '123456' 数字 let num: number = Infinity bool let b:boolean = Boolean(0) void let v:void = undefined any let n:any = null function 阅读全文