1 //第一种泛型接口定义 2 interface ConfigFn<T> { 3 (value:T):T; 4 } 5 6 function test<T>(arg:T):T{ 7 return arg; 8 } 9 //第一种泛型接口的定义使用时需要指定泛型类型 10 let myTestInterFace:ConfigFn<string> = test; 11 myTestInterFace('Test Interface') 12 // myTestInterFace(123) 报错 13 14 15 //第二种泛型接口定义 16 interface ConfigFn2 { 17 <T>(value:T):T; 18 } 19 20 function test2<T>(arg:T):T{ 21 return arg; 22 } 23 24 //第二种泛型接口定义使用时不需要指定泛型类型,可以传入任意类型的值 25 let myTestInterFace2:ConfigFn2 = test2; 26 myTestInterFace2('string'); 27 myTestInterFace2(123456); 28 myTestInterFace2([1,2,3,4,5,6])
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。