Skip to content

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

Description

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

Activity

  1. dnalborczyk commented on Oct 13, 2021

    @dnalborczyk

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

  2. dnalborczyk commented on Oct 13, 2021

    @dnalborczyk

    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.

  3. fatcerberus commented on Oct 13, 2021

    @fatcerberus

    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.

  4. orta commented on Oct 13, 2021

    @orta
    Contributor

    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?

  5. jakebailey commented on Jul 7, 2023

    @jakebailey
    Member

    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

Metadata

Metadata

Labels

DocsThe issue relates to how you learn TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions