| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- (function(){
- // function fn1(x:string):string {
- // return x;
- // }
- // fn1('12');
- // 泛型:用字符去指代未知类型 具体的类型 使用时 在传值
- function fn1<T,K>(xxx:T,y:K):[T,K] {
- return [xxx,y];
- }
- // 当传入实参时 函数中的泛型会自动解析实参的类型
- // fn1(111);
- // fn1('tttt');
- // // 可以再传入参数时 直接声明类型 在<>内
- // fn1<Boolean>(true);
- // fn1('1',false);
- // fn1<string,boolean>('1',false);
- interface happy {
- jump: string
- }
- // 函数 泛型继承接口
- function fn2<T extends happy>(x:T):T {
- return x;
- }
- fn2({jump:'2'})
- //类 泛型继承接口
- class News<T extends happy> {
- xxx:T;
- constructor(x:T) {
- this.xxx = x;
- }
- }
- let news = new News({jump:'2'});
- })()
|