#每天一个知识点#
typescript中 ! 的使用:
1、变量后使用 !,表示类型推断排除null、undefined。例:
const option = treeOptions[0].children![0]!
2、非空类型断言 !.,表示确定某个标识符是有值的,跳过ts在编译阶段对它的检测。例:
function test (value?: string) {
console.log(value!.length);
// console.log(value.length); // 错误提醒: value is possibly 'undefined'.
}
test('hello');// !.不传值, 编译js后会报错, 可以使用?. 替代 !.
展开
评论