TypeScript - 原始类型 Primitive Type

151 阅读1分钟

TS 支持 ECMAscript 的六种原始类型:

  1. string:存放字符串

    const a:string = 'foo';
    
  2. number: 存放数字、NaN、Infinity

    const b:number = 1
    
  3. boolean: 存放布尔值 true | false

    const c:boolean = true
    
  4. void: 函数没有返回值的时候去标记这个函数返回值类型 null | undefined

    function():void {
      ...}
    
  5. null: 空值 null

    const f:null = null
    
  6. undefined: 空值 undefined

    const g:undefined = undefined
    
  7. symbol: Symbol()

string、number、boolean 三个原始类型,在 TS 中默认是可以设置为空值的。也就是说可以给它们赋值 null、undefined

const a:string = null