范型
- 泛型用于提升代码的重用性
范型函数
-
// 定义函数 function hello<T>(arg: T): T { return arg; } // 函数使用方式一 let output = hello<string>('hello typescript'); console.log(output) // hello typescript // 函数使用方式二 let out = hello("hello xcr") console.log(out) // hello xcr
范型变量
-
// 使用方式一 function hello<T>(args: T[]): T[] { console.log(args.length) return args } hello(['hello world']) // 使用方式二 function helloWorld<T>(args: Array<T>): Array<T> { console.log(args.length) return args } helloWorld([1234567, 890231])