【发布时间】:2021-04-21 17:33:23
【问题描述】:
打字稿源代码在很多地方使用undefined!。比如binder.ts,第261行到第271行:
file = undefined!;
options = undefined!;
languageVersion = undefined!;
parent = undefined!;
container = undefined!;
thisParentContainer = undefined!;
blockScopeContainer = undefined!;
lastContainer = undefined!;
delayedTypeAliases = undefined!;
seenThisKeyword = false;
currentFlow = undefined!;
从打字稿官方文档中,后缀! 表示“非空断言运算符”,其定义是:
一个新的!后缀表达式运算符可用于在类型检查器无法断定该事实的上下文中断言其操作数为非空且非未定义
所以undefined! 这种用法似乎没有意义,因为它断言 undefined 是非未定义的。
undefined!是什么意思,我们为什么要这样使用?
【问题讨论】:
-
可能“这在运行时永远不会为空”...
-
@user202729 听起来很合理,但我从来不知道这种用法,希望更多细节或文档
-
可能相关的答案在这里 - stackoverflow.com/questions/38874928/…
标签: javascript typescript eslint tslint