TS 支持 ECMAscript 的六种原始类型:
-
string:存放字符串
const a:string = 'foo'; -
number: 存放数字、NaN、Infinity
const b:number = 1 -
boolean: 存放布尔值 true | false
const c:boolean = true -
void: 函数没有返回值的时候去标记这个函数返回值类型 null | undefined
function():void { ...} -
null: 空值 null
const f:null = null -
undefined: 空值 undefined
const g:undefined = undefined -
symbol: Symbol()
string、number、boolean 三个原始类型,在 TS 中默认是可以设置为空值的。也就是说可以给它们赋值 null、undefined
const a:string = null