0
点赞
收藏
分享

微信扫一扫

记一下typescript中!的用法

我是芄兰 2023-02-09 阅读 64


function TEST(input: string | null): string {
function fix() {
return input!.charAt(0);
}
return fix("test");
}

这个!是断言 说他不为空,如果你不要就会报错

Object is possibly 'null'.ts(2531)

更加耿直的做法

TEST(input: string | null): string {
return input!;
}

如果不加就报错

(parameter) input: string | null

Type 'string | null' is not assignable to type 'string'.
Type 'null' is not assignable to type 'string'.ts(2322)

举报

相关推荐

0 条评论