ts 类型强转

2021-01-28  本文已影响0人  space77

typescript 类型 强行转换

提示:如果不能很好的控制变量类型,我建议不要强转。

ts 关键词:as

未知类型 => 已知类型

type b = { c: number }
const test: any = { a: '1' }
const test1 = test as b
/// test1 的类型类变为b
console.log(test1)

已知的不同类型转换

type b = { c: number }
const test = { a: '1' }
const test1 = (test as any) as b
/// test1 的类型类变为b
console.log(test1)
上一篇 下一篇

猜你喜欢

热点阅读