代码没有问题,为什么typescript编译失败?

通过命令 npx tsdx create packages 生成 typescript + react 项目,某个文件的代码编译不通过:

let test: string | undefined = undefined
test ??= '555' // 编译不能通过
if (typeof test === 'undefined') test = '555' // 换成这样可以通过编译

错误内容如下:

@rollup/plugin-replace: 'preventAssignment' currently defaults to false. It is recommended to set this option to `true`, as the next major version will default this option to `true`.
✓ Creating entry file 1.5 secs
(typescript) Error: syntax error TS1109: Expression expected.

请问这是为什么呢?难道 typescript 不支持 js 的 ??= 运算吗?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

??= 不是检测是不是null嘛

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!