Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript 3.7 Support #903

Closed
loucyx opened this issue Nov 7, 2019 · 21 comments
Closed

TypeScript 3.7 Support #903

loucyx opened this issue Nov 7, 2019 · 21 comments

Comments

@loucyx
Copy link

loucyx commented Nov 7, 2019

I have TypeScript 3.7 installed on a project, and I run tests with ava and ts-node. The issue I found is that compilation works as expected, but tests fail because ts-node doesn't recognize the syntax for nullish coalescence and optional chaining.

So if I have:

foobar?.foo?.bar ?? "default value"

I get errors all over the place. Isn't ts-node using the local typescript package to do compilation?

@keesvanlierop
Copy link

It does, but it is set to 3.6.4 still. Needs to be updated and then good to go I think?

@blakeembrey
Copy link
Member

TypeScript 3.7 is already supported, you would need to update your local installation.

@keesvanlierop
Copy link

keesvanlierop commented Nov 7, 2019

@blakeembrey It's not supported though. As OP mentions, you'll see errors on optional chaining and nullish coalescing expressions when running ts-node, even though your local typescript version is set to 3.7.2.

@loucyx
Copy link
Author

loucyx commented Nov 7, 2019

One of the errors I'm getting:

Uncaught exception in src/tryCatch/tryCatch.test.ts

  ./node_modules/ts-node/src/index.ts:493

  ./src/tryCatch/tryCatch.ts:16
          returnedValue = catchCallback?.(error);
                                        ^

  SyntaxError: Unexpected token '.'

  Module.m._compile (node_modules/ts-node/src/index.ts:493:23)
  module.exports (node_modules/default-require-extensions/js.js:7:9)
  require.extensions.<computed> (node_modules/ts-node/src/index.ts:496:12)

Tried a clean install with npm, still getting errors.

@keesvanlierop
Copy link

keesvanlierop commented Nov 7, 2019

I noticed the fix is to change "target": "esnext" to "target": "es2018" in your tsconfig. No change required here.

@blakeembrey
Copy link
Member

blakeembrey commented Nov 7, 2019

By that error it is compiling correctly. The error is a node.js runtime error, not TypeScript compilation. As noted it’s failing because you’re compiling an output that node.js doesn’t understand yet.

@loucyx
Copy link
Author

loucyx commented Nov 7, 2019

Ohhhh yup. It seems to be a target issue. Thanks @keesvanlierop 😄

@iwasaki-kenta
Copy link

Is there any way to have nullish coalescing work on target esnext? The use case is that bigint's are only supported should esnext be targeted, rather than es2018.

@loucyx
Copy link
Author

loucyx commented Nov 15, 2019

@iwasaki-kenta my solution was to set target to es2018 and then:

"lib": [
      "es2018",
      "esnext.bigint"
    ]

So I have 2018 stuff, but I still support BigInt. It will only work expressed as BigInt("1") not like 1n 😞

@MikeMitterer
Copy link

OMG I had this same problem with a rollup / Vue / TypeScript combination.
"target": "es2018" solved it!

@jardicc
Copy link

jardicc commented Mar 4, 2020

Also "target": "es2019" works for me but "target": "es2020" not yet.

@aqumus
Copy link

aqumus commented Mar 19, 2020

For me "target": "es2019" and "target": "es2020" both works

@NicoCevallos
Copy link

I faced the same issue but the solution depends of the NodeJS version that runs the script.
With NodeJS 12, it worked with "target": "ES2018" and "target": "ES2019", but doesn't work with "target": "ES2020".
But I switched to NodeJS 14 and "target": "ES2020" worked fine.
So pay attention to the NodeJS version you are running your scripts

@alexmacarthur
Copy link

I can also confirm that setting my target to ES2020 didn't work, but ES2019 did do the trick. I'm running Node v10.15.3.

@archywillhe
Copy link

this is going backwards it is werid.

@cspotcode
Copy link
Collaborator

Remember that TypeScript is compiling your code from .ts into .js, not ts-node. We simply use the compiler API, and the returned code is executed by node. What does your node version do when it executes the code being emitted from TypeScript? What kind of code is being emitted based on your compiler configuration?

If you set "target": "esnext", what code is TypeScript emitting? What does your node version do when it tries to execute this code?

Make sure you know the answers to these question. Otherwise, you're likely to confuse yourself and be unsure whether a problem is caused by your configuration or by ts-node. This will waste time and is probably not what you want.

@jsonchou
Copy link

nullish coalescing and optional chaining are supported in node >= v14.0.0

@kierans
Copy link

kierans commented Nov 9, 2020

Just to make things a bit easier for new players, remember that "The target setting changes which JS features are downleveled", that is if the feature isn't natively supported by the NodeJS runtime version the TS compiler generates different code that will run on the node version. That's why es2020 works on node >= 14 but doesn't on node < 14

@zWingz
Copy link

zWingz commented Nov 26, 2020

Also "target": "es2019" works for me but "target": "es2020" not yet.

@jedwards1211
Copy link

I think @babel/preset-env was pretty wise to support targets: { node: 'current' }. Too bad that tsconfig.json doesn't!

jedwards1211 added a commit to jedwards1211/ts-node that referenced this issue Jan 28, 2021
as discussed in TypeStrong#903.  I had to search for awhile to find that issue and figure out what was going on, and there were some duplicate issues, so I think it would help people to explain this directly in the README.
@Thinking80s
Copy link

I noticed the fix is to change "target": "esnext" to "target": "es2018" in your tsconfig. No change required here.

why config to "target": "es2018" is ok?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests