1、泛型的概念

  1. /* 泛型:任意类型 */
  2. //any
  3. var str1:any = 'hello'
  4. str1 = 2212
  5. // 劣势:放弃了类型检查
  6. // 即要任意类型又要类型检查
  7. // 1、有类型检查,只能放置特定数据类型
  8. function getData(msg:string){
  9. console.log(msg);
  10. }
  11. // 2、放弃了类型检查,但是能够传任意类型
  12. function test1(msg:any){
  13. console.log(msg);
  14. }
  15. test1(222)
  16. // 3、泛型,既有类型检查,又可以传任意类型,这也是泛型的好处所在
  17. function getMsg<T>(msg:T){
  18. console.log(msg);
  19. }
  20. getMsg<string>("hello")
  21. getMsg<number>(121)