Ts-node: Support for TS 3.7 (Optional Chaining, Nullish Coalescing)

Created on 8 Nov 2019  路  8Comments  路  Source: TypeStrong/ts-node

Hi there,

I just tried to upgrade my app to TypeScript 3.7 but I just got stuck on ts-node (8.4.1) not recognising the syntax of both Optional Chaining (obj?.prop) and Nullish Coalescing (const value = val1 ?? val2).

Since I didn't see any issue related to this, do you know if there is someone already working on it ?
I could try to help with this, even though I don't have any experience with ts-node code base.

invalid

Most helpful comment

If someone get to this the problem is in tsconfig , namely you've chosen "target":"esnext" it should be "es2020" or "es2019"

All 8 comments

What鈥檚 the error? It鈥檚 most likely your configuration, this package supports any typescript version you鈥檙e using.

`const data = result?.rows;

SyntaxError: Unexpected token .
at Module._compile (internal/modules/cjs/loader.js:703:23)
at Module.m._compile (./node_modules/ts-node/src/index.ts:493:23)
at Module._extensions..js (internal/modules/cjs/loader.js:770:10)
at Object.require.extensions. [as .ts] (./node_modules/ts-node/src/index.ts:496:12)
at Module.load (internal/modules/cjs/loader.js:628:32)
at Function.Module._load (internal/modules/cjs/loader.js:555:12)
at Module.require (internal/modules/cjs/loader.js:666:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object. (./service/markerService.ts:27:1)
at Module._compile (internal/modules/cjs/loader.js:759:30)`

I use nodemon with this config :
{ "watch": [ "src" ], "ext": "ts,json,d.ts,js", "delay": "3", "execMap": { "ts": "ts-node --files src/server.ts" }, "ignore": [ "src/**/*.spec.ts" ] }

image

Same error with Nullish Coalescing syntax.

This is a runtime error, not a TypeScript error. Duplicate of https://github.com/TypeStrong/ts-node/issues/903.

If someone get to this the problem is in tsconfig , namely you've chosen "target":"esnext" it should be "es2020" or "es2019"

I am also having trouble getting optional chaining to work with ES2020, but it does work with ES2019. Problem is I need Promise.settleAll() which doesn't work with ES2019, only ES 2020, so I'm in cacth 22

node ver: 12.18

"typescript": "4.0.2"
"ts-node": "9.0.0",

// tsconfig 
"compilerOptions": {
  "target": "ES2020"
}

Error:
```
const isOwner = context.getUserId() === kanban?.userId;

SyntaxError: Unexpected token '.'

@clairefro you should be able to set "lib" to include es2020 type declarations and "target" to downlevel to es2019.

If you have any more questions, I recommend asking on the TypeScript Community Discord. https://discord.com/invite/typescript

@clairefro same thing March 13, 2021, I hope I could have time to provide a solution for this feature, but I don't, I got many things to do, kudos and thanks to all the people working hard on this, as a dev I appreciate the effort, but for now, I will wait for a solution using ES2020, my problem is fixed by using ES2019, but I don't know why yet, I am open to explanations 鉁岋笍.

Check out the configs from @tsconfig/bases. Look at the target and lib options.
https://github.com/tsconfig/bases

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mattdell picture mattdell  路  4Comments

grissius picture grissius  路  3Comments

conordickinson picture conordickinson  路  4Comments

Borewit picture Borewit  路  3Comments

aj-r picture aj-r  路  3Comments