类型注解和类型推断
type annotation 类型注解
const count:number (count:number类型注解)
count = 123
type inference 类型推断
const count = 123 (ts解析引擎可以根据123推断出count的类型是number)
const a = 1
const b = 2
const c = a+b (可以推断出c为number类型)
function getTotal(one,two) {
return one+two
}
const total = getTotal(1,2)
// 此种形式无法推断total
function getTotal(one:number,two:number) {
return one+two
}
const total = getTotal(1,2)
// 此种形式可以推断total为number
// 推断对象的属性类型
const obj = {
name:'test',
age:19
}
// 可以推断出name的类型是string,age的类型是number