Search code examples
typescriptsafaribackwards-compatibilitysnowpack

Typescript + snowpack is outputting nullish coalescing operators in target < ES2020


I'm trying to compile my typescript code/packages to be compatible with Safari Version < 14.

From what I've found, this means no nullish coalescing operators (??) allowed. It seems that the support for them was added is ES2020, so I lowered my target output to ES2019. However, no matter how low I go, typescript/snowpack (optimizations off, sourcemap on) continues to generate ??'s in the output directory, both in my code and in the code for third party packages.

In fact, in my testing, it seems that Safari 13 doesn't even support the alternative method of using || such as let x = a || b.

Is there any way around this, or am I missing something?


Solution

  • Turn out, snowpack does not look at the tsconfig.json at all when building. It uses its own toolchain to compile TypeScript.

    The setting to specify the target for the compiled JS is in the optimize config property. However, this property is not documented in the configuration documentation, but in a separate section about optimization and bundling. Specifying optimize.target to "es2019" results in the behavior I'm looking for. No more ?? operators in the output.