介绍
TypeScript 是微软开发推出的语言,是 JavaScript 的超集,补充了静态类型检查等功能
大多数情况下,TypeScript 需要编译为 JavaScript 再运行
类型
模块
用 import 和 export 来导入和创建模块
1 2 3 4
| export function test() { console.log('hello world') }
|
命名空间
用 namespace 将具有相似功能或属性的类、接口等进行分组,避免命名冲突的方式
1 2 3 4 5 6 7 8 9 10
| namespace Test { export function test() { console.log('hello world') } }
Test.test()
|
类型守卫
运行时的类型断言
1 2 3 4
| const test: any = 'sss' if (typeof test === 'string') { }
|
Interface
接口定义,可以用来定义接口、函数和数组
1 2 3 4 5 6 7 8 9
| interface Fun { (a: number): void }
interface Arr { [index: number]: string }
|
装饰器
装饰器是一种特殊类型的声明,可以附加到类、方法、访问符、属性或参数上,以修改其行为。在 TypeScript 中,装饰器提供了一种在声明时定义如何处理类的方法、属性或参数的机制。
1 2 3 4 5 6 7 8 9 10
| function classDecorater<T extends { new (...args: any): {} }>(constructor: T) { return class extends constructor { public test = 'xxxx' } }
@classDecorater class Test { public aaa = 'aaa' }
|
1 2 3 4 5 6 7 8 9 10 11
| function propDecorater() { return function (obj: any, key: string, config: PropertyDescriptor) { console.log('obj', obj, 'key', key, 'config', config) } }
class Test { @propDecorater() aaa: string }
|
内置类型实现
1
| type MyExclude<T, K> = T extends K ? never : T
|
1
| type MyInclude<T, K> = T extends K ? T : never
|
1
| type MyOmit<T extends Object, K> = Pick<T, Exclude<keyof T, K>>
|
1
| type MyPartial<T extends Object> = { [K in keyof T]?: T[K] }
|
1 2 3
| type MyPick<T extends Object, K> = { [M in MyInclude<keyof T, K>]: T[M] }
|
1
| type MyRequired<T> = { [K in keyof T]-?: T[K] }
|
1 2 3
| type MyParameters<T extends (arg: any) => any> = T extends (arg: infer K) => any ? K : never
|
1
| type MyConstructorParameters<T extends abstract new(arg: any): any > = T extends abstract new(arg: infer K): any ? K : never
|
1 2 3
| type MyReturnType<T extends (arg: any) => any> = T extends (arg: any) => infer K ? K : never
|
场景
1 2 3
| function getPropType<T, K extends keyof T>(obj: T, key: K) { return obj[key] }
|
1
| type NotNull<T> = T extends null | undefined ? never : T
|
1 2 3 4 5
| function call<T extends `aaa_${string}`>(a: T) { console.log('aa', a) }
call('xxx')
|
1 2 3 4 5 6 7
| type test<T extends number> = `${T}` extends `-${infer M}` ? never : T
function testCall<T extends number>(a: test<T>) { console.log('testCall', a) }
testCall(-1)
|
参考