NOTE: This plugin is included in
@babel/preset-env, in ES2020
In
var foo = object.foo ?? "default";
Out
var _object$foo;
var foo =
(_object$foo = object.foo) !== null && _object$foo !== void 0
? _object$foo
: "default";
NOTE: We cannot use
!= nullhere becausedocument.all == nullanddocument.allhas been deemed not "nullish".
npm install --save-dev @babel/plugin-proposal-nullish-coalescing-operator
yarn add --dev @babel/plugin-proposal-nullish-coalescing-operator
pnpm add --save-dev @babel/plugin-proposal-nullish-coalescing-operator
{
"plugins": ["@babel/plugin-proposal-nullish-coalescing-operator"]
}
babel --plugins @babel/plugin-proposal-nullish-coalescing-operator script.js
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-nullish-coalescing-operator"],
});
loose
boolean, defaults to false.
When true, this transform will pretend document.all does not exist, and perform loose equality checks with null instead of strict equality checks against both null and undefined.
⚠️ Consider migrating to the top level
noDocumentAllassumption.
{
"assumptions": {
"noDocumentAll": true
}
}
In
var foo = object.foo ?? "default";
Out
var _object$foo; var foo = (_object$foo = object.foo) != null ? _object$foo : "default";
You can read more about configuring plugin options here
© 2014-present Sebastian McKenzie
Licensed under the MIT License.
https://babeljs.io/docs/babel-plugin-proposal-nullish-coalescing-operator/