wangdoc / typescript-tutorial

TypeScript 教程
https://wangdoc.com/typescript
2.43k stars 256 forks source link

类型断言 #100

Closed xiaoqufengdi closed 6 months ago

xiaoqufengdi commented 6 months ago

大神,类型断言那一章,联合类型断言 const s1:number|string = 'hello'; const s2:number = s1 as number; // 报错

值实际是字符串断言成number会报错, 应该断言成字符串,或者用unkonwn中转一下断言成number const s2:string= s1 as string; const s22: number = s1 as unknown as number

ruanyf commented 6 months ago

谢谢指出,已经改正