记一下typescript中!的用法

阅读 64

2023-02-09


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)

0 0 举报