一,数字类型
1.数字类型:number
let a:number = 123
let b:number = 0b1010
二,字符串类型
1.字符串类型:string
let a:string = "123"
三,布尔类型
1.布尔类型:boolean
let a:boolean = true
四,空和未定义
1.null
和undefined
2.在ts
中,undefined
和null
两者各自有自己的类型分别叫做undefined
和null
,null
和undefined
是所有类型的子类型,当在tsconfig.json => strict为false
可以赋值给其他类型的变量 ,strict为true
不可以,实际上也不建议将undefined
赋值给其他类型。
let a:undefined = undefined
let b:null = null
//strict:false 时
let c:number = a // 不会报错
五,任意类型
1.任意类型:any
,可以被赋值为任意值,也脱离了typescript
的类型检测系统
let a:any = 'hello world'
a = 123 //赋值为不同的类型不会报错
a.length //也不会报错,所以,我们应该尽量的少用any
六,void
1.在ts
中void
表示空值,只在没有返回值的函数中使用,只能赋值undefined
或者null
function add():void{
console.log(111)
}
七,注意事项
1.注意区分基础类型和内置对象,例如number
和Number
是不一样的
let a:number= 1
let b:Number= new Number(1)