TypeScript 中的 nullish 合并运算符 (??) 是否比 JavaScript 有更多的浏览器支持?

Does the nullish coalescing operator (??) in TypeScript have more browser support than JavaScript's?

在 JavaScript 中,browser support for the nullish coalescing operator (??) 仅限于较新的浏览器(例如 Chrome 80、Edge 80、Firefox 72)。由于 TypeScript 被转换为 JavaScript,nullish 合并运算符是否也经过某种转换,有点像 polyfill?

do nullish coalescing operators go through some sort of conversion as well, sort of like a polyfill?

TypeScript 被转译为 JavaScript。至于现在,是的,nullish 合并将被转译,连同您的 target ES 版本在您的 tsconfig.

中尚不支持的所有其他语法一起被转译。

例如,在 TS 中:

obj.foo ?? 5;

被转译为

"use strict";
var _a;
(_a = obj.foo) !== null && _a !== void 0 ? _a : 5;

同理,求幂运算符:

3 ** 5

被转译为

Math.pow(3, 5);

如果您的目标是 ES2015 或更早版本。 (求幂运算符是在 ES2016 中引入的。)否则,如果您的目标是 ES2016 或更高版本,它不会被转译。