为什么打字稿将这种无效合并转换为三元?

问题描述 投票:0回答:1

我正在开发一个 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 :  : 
)?

我尝试在无效合并周围添加括号,但它不会改变输出。

angular typescript
1个回答
0
投票

确保您使用的 TypeScript 版本支持 nullish 合并运算符。 TypeScript 3.7 中引入了 nullish 合并运算符。

© www.soinside.com 2019 - 2024. All rights reserved.