我正在开发一个 Angular 应用程序,我有以下代码:
// ...
this.x = y ?? z;
// ...
当我运行
ng serve
时,我的终端中出现以下错误:
error TS1109: Expression expected.
error TS1005: ':' expected.
Module parse failed: Unexpected token (47:40)
You may need an appropriate loader to handle this file type.
| // ...
| this.x = y ? ? z : : ;
| // ...
为什么要将空合并 (
y ?? z
) 转换为三元 (y ? ? z : :
)?
我尝试在无效合并周围添加括号,但它不会改变输出。
确保您使用的 TypeScript 版本支持 nullish 合并运算符。 TypeScript 3.7 中引入了 nullish 合并运算符。