Support optional chaining by default #35278
Replies: 5 comments 1 reply
-
Hello! I think the big focus of the Next.js team is on SWC (which is enabled by default) See https://nextjs.org/docs/advanced-features/compiler and https://swc.rs/docs/migrating-from-babel#comparison |
Beta Was this translation helpful? Give feedback.
-
It seems like If you need to include any other babel plugins/presets, you'll need to make sure you include |
Beta Was this translation helpful? Give feedback.
-
Cool then, ty for the info! It seems I was a little late 😂 |
Beta Was this translation helpful? Give feedback.
-
The question is - how long do we need to transpile that? All browsers support https://caniuse.com/?search=optional%20chaining adding for eaxmple console.log(window?.some?.property?.value);
const x = window.foo?.bar ?? null is transpiled to: var ref, ref, ref;
console.log(window === null || window === void 0 ? void 0 : (ref = window.some) === null || ref === void 0 ? void 0 : (ref = ref.property) === null || ref === void 0 ? void 0 : ref.value);
var ref;
const x = (ref = (ref = window.foo) === null || ref === void 0 ? void 0 : ref.bar) !== null && ref !== void 0 ? ref : null; |
Beta Was this translation helpful? Give feedback.
-
Does anyone know to polyfill optionalChaining with swc in a NextJS setup? |
Beta Was this translation helpful? Give feedback.
-
Describe the feature you'd like to request
I have had many can't access property of undefined errors and I could figure out why, although I have been using optional chaining. After some search I found the reason and also the solution. I believe many people are affected by this, and are probably losing their minds like I was, because they can't figure out why this is happening :(
I know this is already a known bug but I would like to propose adding Babel's solution for this by default, as explained here: @babel/plugin-proposal-optional-chaining
I don't know if you already are aware of this, but didn't do it because it may affect something else. However I noticed this and thought it would be beneficial to be added. If I'm wrong just ignore this 👍
Describe the solution you'd like
It's just a matter of installing the package provided by Babel, like so:
and then adding this code in a .babelrc config file
Describe alternatives you've considered
That would already be all :D
Beta Was this translation helpful? Give feedback.
All reactions