ts-loader: Can't use nullish coalescing operator with TypeScript 3.8.2

Expected Behaviour

With TypeScript 3.8.2 I can’t use the nullish coalescing operator ?? anymore.

Actual Behaviour

I’m getting the error message

Module parse failed: Unexpected token (36:49)
File was processed with these loaders:
 * ./node_modules/ts-loader/index.js
You may need an additional loader to handle the result of these loaders.

Steps to Reproduce the Problem

Just use const foo = bar ?? '' in your codebase. With TypeScript 3.7.5 everything works fine

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

No. It’s not a bug. It’s a breaking change in the compiler behavior. I changed the target to es2019 and it’s working now.

Oh, I got it. typescript 3.8 does non transform nullish syntax but webpack’s acorn can not parse. So ts-loader(and webpack) requires es2019 implicitly.

On my side, I am using typescript 4.3.5, node between >=14.16 and <15, es2020 and I also got the issue with nullish chaining operator. ts-loader version is: 8.0.7 and I need es2020 beacuse of bigint. Is there a solution for that config ?

@MadhuvanthG Actually I’m currently in v15.10.0, and I still have the problem.

Same here with "typescript": "^4.1.3" still needed to downgrade my "target": "ESNext" to "target": "ES2019" in order to use nullish coalescing along with "poi": "^12.10.3" & "@poi/plugin-typescript": "^12.3.1", poi use webpack under the hood

I encountered same problem with typescript@3.8.3

target:es2019 works but target:es2020 is broken. Maybe target:es2020 is something wrong.

Why is this issue closed? I want optional chaining and nullish coalescing to appear in the outputted code because other wise, it messes up my code coverage. I am building an electron app and so it supports these latest features.

Then you need a Node.js version who is able to bundle code with nullish coalescing. At the moment this is not supported by Node.js

To add info to this thread, you can use “ES2020” as your target if you are on Node 14 or higher. Node14 does support nullish coalescing and it’s in Active LTS at the moment, so safe to use in Production as well.

Why is this issue closed? I want optional chaining and nullish coalescing to appear in the outputted code because other wise, it messes up my code coverage. I am building an electron app and so it supports these latest features.

You can get it with babel if you want it now: https://www.npmjs.com/package/@babel/plugin-proposal-nullish-coalescing-operator

I don’t totally get it. What if I really want es2020, as I’m building solely for the latest Chromium which supports it natively?

Same problem here when using the nullish coalescing operator.

fork-ts-checker-webpack-plugin and vscode @ 3.8 things all are ok.

For me TypeScript 3.7.5 also doesn’t work. @screendriver what version of ts-loader are you using with TypeScript 3.7.5 that does work?