类型推断
let str = 'hello'
str = 100
自动推断为字符串,赋值为数字就报错
Type 'number' is not assignable to type 'string'.
类型注解
let str: string = 'hello'
let str2: string
str2 = 'world'
类型断言
let num = [1, 2, 3]
const res = num.find(item => item > 2)
console.log(res * 100)
会提示:
'res' is possibly 'undefined'.
可以使用as
断言,find
执行后的结果一定是数字
const res = num.find(item => item > 2) as number
TS 基础类型
let v1: string = 'hello'
le...