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 OverflowOptional 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!
The optional chaining operator is in Stage 4 which hasn't reached cross-browser stability. It's not supported by IE.
You could try to use @babel/plugin-proposal-optional-chaining to transform it. It can transform optional chaining operators into a series of nil checks.
See this example on TypeScript Playground:
const obj: {
outer?: {
inner?: string
}
} = {};
console.log(obj?.outer?.inner);
Playground Link
Even when targeting ES2017, the optional chaining operator is compiled to basic JS code which all current browsers understand.
So no need to worry :)
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,
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.