Typescript: optional chaining transpilation

Is there a way to force Typescript not to transpile optional chaining? Javascript supports this syntax in modern browsers.

Assuming you are referring to this Typescript feature, you seem to be asking if you can transpile in such a way that the optional chaining is preserved in the JavaScript.

Before answering, may I ask why you would care? The whole point of transpiling is actually that you should not care about the JavaScript produced (except perhaps in the case that the transpiler has a bug).

The direct answer to your question is probably yes, by targeting a different version of JavaScript either via CLI (using --target) or tsconfig. According to this website, JavaScript added support for optional chaining in ES2020 so you should set the target to es2020 or higher.

It is also worth noting that IE still does not support optional chaining today, but if you do not have to support IE you should be fine.

1 Like

I just want to know how this will apply for a long-term project with a big codebase.
In particular, how I will reduce transpiled code over time.

Thank you for your answer. :+1: Now it’s clear. Will use ‘target’ in tsconfig.

1 Like

Just some friendly advice: avoid premature optimization. I would not worry about it too much unless it proves to be an actual issue.