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

Recommended tsconfig.json target option for Node 14 is ES2020 which is not fully supported by Node 14. #46325

Closed
ansavchenco opened this issue Oct 12, 2021 · 5 comments
Assignees
Labels
Docs The issue relates to how you learn TypeScript
Milestone

Comments

@ansavchenco
Copy link

Bug Report

🔎 Search Terms

node 14, tsconfig.json, es2020, recommended

Problem

According to the Node Target Mapping wiki page the recommended tsconfig.json for Node 14 is:

{
  "compilerOptions": {
    "lib": ["ES2020"],
    "module": "commonjs",
    "target": "ES2020"
  }
}

However, if we look at the node.green ES2020 feature support table we will see that Node 14 does not support one feature of ES2020 which is "spread parameters after optional chaining".

⏯ Playground Link

Playground link with relevant code

💻 Code

input.ts:

type Fn = ((...args: unknown[]) => void) | null;
type O = { method?: (...args: unknown[]) => void };
type N = { method: (...args: unknown[]) => void } | null;

var fn: Fn = null;
var n: N = null;
var o: O = {};

function func(fn: Fn, n: N, o: O) {
  return (
    fn?.(...[], 1) === void undefined &&
    fn?.(...[], ...[]) === void undefined &&
    o.method?.(...[], 1) === void undefined &&
    n?.method(...[], 1) === void undefined
  );
}

console.log(func(fn, n, o));

Output JavaScript:

"use strict";
var fn = null;
var n = null;
var o = {};
function func(fn, n, o) {
    return (fn?.(...[], 1) === void undefined &&
        fn?.(...[], ...[]) === void undefined &&
        o.method?.(...[], 1) === void undefined &&
        n?.method(...[], 1) === void undefined);
}
console.log(func(fn, n, o));

🙁 Actual behavior

/Users/antonsavcenko/Development/misc/typescript/src/index.js:5
    return (fn?.(...[], 1) === void undefined &&
    ^

TypeError: Function.prototype.apply was called on null, which is a object and not a function
    at func (/Users/antonsavcenko/Development/misc/typescript/src/index.js:5:5)
    at Object.<anonymous> (/Users/antonsavcenko/Development/misc/typescript/src/index.js:10:13)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

🙂 Expected behavior

true
@andrewbranch andrewbranch added Docs The issue relates to how you learn TypeScript Needs Investigation This issue needs a team member to investigate its status. labels Oct 12, 2021
@andrewbranch andrewbranch added this to the Backlog milestone Oct 12, 2021
@dnalborczyk
Copy link

confirmed. I wonder if this was essentially a bug which was discovered and fixed later in V8 and made it only into node v16+. 🤔

@dnalborczyk
Copy link

dnalborczyk commented Oct 13, 2021

seems like it:

https://bugs.chromium.org/p/v8/issues/detail?id=11558

babel/babel#13001

compat-table/compat-table#1708

that said, I think ES2020 is still a pretty good target recommendation for node.js v14 (unless of course, one is using code similar like the above). it's probably not uncommon that bugs like that are being found and fixed after any initial ecma support releases of V8 AND typescript.

@fatcerberus
Copy link

Not surprised it turned out to be a bug - the error message mentions Function.prototype.apply which isn't even used in the example code, and unsupported syntax should produce a SyntaxError, not an obtuse runtime error like this.

@orta
Copy link
Contributor

orta commented Oct 13, 2021

Yeah, even knowing this I think es2020 is a safe enough call vs recommending es2019 👍🏻 - thanks for the pointer. We could add a note to the wiki page though?

@orta orta removed the Needs Investigation This issue needs a team member to investigate its status. label Oct 13, 2021
@jakebailey
Copy link
Member

jakebailey commented Jul 7, 2023

I've gone and updated https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping to mention this issue and suggest lowering target to ES2019 if this feature is needed.

With the EOL of Node 14 and the immanent EOL of Node 16 (which was only affected in <16.3.0, which nobody should be using), I think that people are very unlikely to hit this.

(TS itself targets ES2020 despite officially supporting Node 14!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants