These patches don't exist in core-js.

More over, this can't be polyfiled as both are syntactic sugar. Babel isn't polyfilling but transpiling.

Answer from Matthieu Riegler on Stack Overflow
🌐
Reddit
reddit.com › r/webdev › js optional chaining polyfill?
r/webdev on Reddit: JS Optional Chaining Polyfill?
October 24, 2019 -

Optional chaining is one of the things I like the most about writing in Swift, and I want to bring that code cleanliness to my React and React native projects.

Does anyone know of a polyfill for unsupported browsers, specifically mobile browsers and Safari? I’m not sure if Babel solves this to be honest.

Thanks!

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript | MDN
This results in shorter and simpler expressions when accessing chained properties when the possibility exists that a reference may be missing. It can also be helpful while exploring the content of an object when there's no known guarantee as to which properties are required. For example, consider an object obj which has a nested structure. Without optional chaining, looking up a deeply-nested subproperty requires validating the references in between, such as:
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › objects: the basics
Optional chaining '?.'
This is a recent addition to the language. Old browsers may need polyfills. The optional chaining ?.
🌐
DEV Community
dev.to › tusharshahi › cost-of-unnecessary-optional-chaining-and-nullish-coalescing-operator-3h09
Cost of unnecessary Optional Chaining (and Nullish coalescing operator) - DEV Community
April 6, 2024 - As we can see from the docs, both are recent additions to the language. This means to get them to work on older browsers, they need polyfills.
🌐
DEV Community
dev.to › slashgear_ › optional-chaining-trap-3820
😰 Optional chaining trap ! - DEV Community
July 31, 2019 - So I still believe Babel is the better option, both in terms of performance and bundle size. That's not to say that it won't add any size to your bundle, but then all polyfills do.
🌐
Babel
babeljs.io › presets › @babel/preset-env › optional-chaining
@babel/plugin-transform-optional-chaining · Babel
const obj = { foo: { bar: { baz: 42, }, }, }; const baz = obj?.foo?.bar?.baz; // 42 const safe = obj?.qux?.baz; // undefined // Optional chaining and normal chaining can be intermixed obj?.foo.bar?.baz; // Only access `foo` if `obj` exists, and `baz` if // `bar` exists // Example usage with bracket notation: obj?.["foo"]?.bar?.baz; // 42
🌐
GitHub
github.com › tc39 › proposal-optional-chaining
GitHub - tc39/proposal-optional-chaining
Let’s call Optional Chain an Optional Chaining operator followed by a chain of property accesses, method or function calls.
Starred by 4.9K users
Forked by 72 users
Languages   HTML 97.2% | Shell 2.8%
🌐
DEV Community
dev.to › smeijer › the-costs-of-optional-chaining-5bno
The costs of Optional Chaining. - DEV Community
May 29, 2023 - You're right. It's definitely the polyfill causing this. In the jsperf link I've posted, you can also see that I did compare it with idx. And both have this issue.
Find elsewhere
🌐
GitHub
github.com › vercel › next.js › discussions › 35278
Support optional chaining by default · vercel/next.js · Discussion #35278
I think the big focus of the Next.js team is on SWC (which is enabled by default) SWC does support optional chaining, making it easier for people to use it by default.
Author   vercel
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1352395 › optional-chaining-in-not-working-in-node-v18-azure
Optional chaining in not working in Node V18 azure web app - Microsoft Q&A
Upgrade the Node.js version on App Service to version 18.7.0 or higher. Optional chaining was introduced in Node 18.7. Use a polyfill library like optional-chaining-polyfill to add support for older Node versions.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-optional-chaining-explained
JavaScript Optional Chaining `?.` Explained - How it Works and When to Use it
August 25, 2020 - let familyTree = { us: { children: {} } } // with _.get const grandChildren = _.get(familyTree, 'us.children.theirChildren', 'got no kids' ); //with optional chaining and null coalescing const nullCoalescing = familyTree?.us?.children?.theirChildren ?? 'got no kids' console.log(nullCoalescing) //got no kids · It also works for objects that may be null or undefined: ... Try it in your browser's console: This is a recent addition and old browsers may need polyfills.
🌐
GitHub
github.com › agokhale › descend
GitHub - agokhale/descend: javascript optional chaining polyfill
javascript optional chaining polyfill . Contribute to agokhale/descend development by creating an account on GitHub.
Author   agokhale
🌐
Interlinked
blog.interlinked.us › 69 › nullish-coalescing-and-optional-chaining
Nullish Coalescing and Optional Chaining - InterLinked Blog
So, why does it matter that N.C can't be polyfilled? Because there's no way to make older browsers support it. Quite, literally, if your codebase uses N.C. at all, anywhere, your entire webpage is suddenly partially or completely incompatible with any browser - any user agent at all - that does not natively support the N.C. operator. Same goes for optional chaining (O.C.).
🌐
Can I Use
caniuse.com › mdn-javascript_operators_optional_chaining
JavaScript operator: Optional chaining operator (`?.`) | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
🌐
CSS-Tricks
css-tricks.com › the-optional-chaining-operator-modern-browsers-and-my-mom
The Optional Chaining Operator, “Modern” Browsers, and My Mom | CSS-Tricks
February 2, 2022 - And that’s why I minimize code, add polyfills, never put raw code. Rollup to libraries, webpack to full pages and browserlist last 2 browsers.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-optional-chaining
How to Use Optional Chaining in JavaScript
February 7, 2022 - Optional chaining was introduced in ES2020. According to TC39 it is currently at stage 4 of the proposal process and is prepared for inclusion in the final ECMAScript standard. This means that you can use it, but note that older browsers may still require polyfill usage.
Top answer
1 of 2
28

The problem is you are targeting esnext this will tell the compiler to output all language features as is without any transpilation. Set the language to es2020 (or below) and ?. and ?? will get transpiled to compatible code:

Copy(async function () {
    let imageFileId = (await db.query(sql`select id from image_files where sha256=${sha256}`))[0]?.id;
})()

Playground Link

There is no fine-grained control over which language features get transpiled and which don't do you have to pick a version as a whole unfortunately,

2 of 2
5

Well, I didn't want to use Babel because then I'd have to figure out how to replace ts-node. There's a bunch of outdated docs out there referring to old Babel packages, but these instructions should work as of Nov 2019:

Add a .babelrc file:

Copy{
    "presets": [
        ["@babel/preset-env",{"targets": {"node": "current"}}],
        "@babel/preset-typescript"
    ],
    "plugins": [
        "@babel/plugin-syntax-bigint"
    ]
}

Add these deps:

Copy  "devDependencies": {
    "@babel/cli": "^7.7.0",
    "@babel/core": "^7.7.0",
    "@babel/node": "^7.7.0",
    "@babel/plugin-syntax-bigint": "^7.4.4",
    "@babel/preset-env": "^7.7.1",
    "@babel/preset-typescript": "^7.7.0",
    "@types/node": "^12.7.5",
    "typescript": "^3.7.2"
  }

Execute your code with:

Copynode_modules/.bin/babel-node --extensions ".ts" src/index.ts

The --extensions ".ts" is very important, even though you're explicitly trying to execute a .ts file, it won't transpile it w/out that.

I like to use GNU Make instead of package.json scripts:

CopyMAKEFLAGS += --no-builtin-rules
.SUFFIXES:
NM := node_modules/.bin
.PHONY: build start dev clean test publish

## commands
########################################

__default:
    $(error Please specify a target)

build: build-types build-js dist/package.json

build-types: node_modules/.yarn-integrity
    $(NM)/tsc --emitDeclarationOnly

build-js: node_modules/.yarn-integrity
    $(NM)/babel src --out-dir dist --extensions ".ts" --source-maps inline

run: node_modules/.yarn-integrity
    $(NM)/babel-node --extensions ".ts" src/index.ts

check: node_modules/.yarn-integrity
    $(NM)/tsc --noEmit

dist:
    mkdir -p $@

clean:
    rm -rf node_modules dist yarn-error.log

dist/package.json: package.json | dist
    jq 'del(.private, .devDependencies, .scripts, .eslintConfig, .babel)' $< > $@

## files
########################################

node_modules/.yarn-integrity: yarn.lock
    @yarn install --frozen-lockfile --production=false --check-files
    @touch -mr $@ $<

yarn.lock: package.json
    @yarn check --integrity
    @touch -mr $@ $<

Or just copy from Microsoft's TypeScript Babel Starter.

🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › type › optional chaining and nullish coalescing
How can I use optional chaining and nullish coalescing in my JavaScript project? - 30 seconds of code
June 12, 2021 - The optional chaining operator (?.) allows us to access deeply nested object properties without having to validate each reference in the nesting chain. In case of a reference being nullish (null or undefined) the optional chaining operator will ...