1.类型推断
我声明了一个变量但是没有定义类型
TypeSctipt会在没有明确类型的时候推断出一个类型。这就叫类型推断。
let str = 'hello' // 默认成string类型
let a // 默认成any类型
2.类型别名
type 关键字(可以给一个类型定义一个名字)多用于复合类型
定义类型别名
type str = string
let s:str = "hello"
console.log(s);
定义函数别名
type str = () => string
let s: str = () => "hello"
console.log(s);
定义联合类型别名
type str = string | number
let s: str = 123
let s2: str = '123'
console.log(s,s2);
定义值的别名
type value = boolean | 0 | '213'
let s:value = true
//变量s的值 只能是上面value定义的值
type和interface开始的时候总觉得他们是一样的,后面发现,接口只可以定义对象类型来约束对象 而type可以定义任意的类型 type可以定义联合类型、交叉类型、甚至是一个属性类型。