# 常用的ts语法有哪些

  • type 类型别名

  • interface

  • enum

  • class

  • 联合类型

  • 泛型

  • 断言

  • 声明文件 declare

    declare modlue '*.vue' {
     import Vue from 'vue'
     export Vue
    }
    
    1
    2
    3
    4
  • 全局变量

    declare const mock: bolean
    declare const CODE_VERSION: string
    
    1
    2

# type和interface的区别

  • 共同点

    • 都能描述一个对象

    • 都能实现继承,也可以相互继承

      interface A = {}
      interface B = {}
      type c = xxx
      type d = yyy
      
      A extends B
      c & d
      
      A extends c
      d & A
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
  • 不同点

    • type是对类型的重新定义

    • type可以约束基本类型,interface不行

      type numberOrString = number | string
      
      1
    • type 的继承用合并

      type a = number
      type b = string
      
      b & a
      
      1
      2
      3
      4
    • interface 有声明合并

最后更新时间: 2/24/2023, 7:57:29 AM