microsoft / microsoft/TypeScript
TS2401 does not work with `"target": "ESNext"`
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
TS2401 ESNext return super
🕗 Version & Regression Information
- This is the behavior in every version I tried (~4.9, ~5.0, ~5.1) (TS2401 seems to appear in 4.9)
⏯ Playground Link
Stackblitz link with relevant code (does not appear in Playground, appears in VSCode & Stackblitz )
Comment out "target": "ESNext" in tsconfig.json and the error will appear
💻 Code
class MyArray extends Array {
constructor(public foo?: any) {
return !foo ? [] : (super() as any);
// ^^^^^^^
// no error with `"target": "ESNext"`, otherwise:
// A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.(2401)
}
}
new MyArray();
🙁 Actual behavior
No error when "target": "ESNext"
The compiled code causes runtime error Must call super constructor in derived class before accessing 'this' or returning from derived constructor
🙂 Expected behavior
TS2401 when "target": "ESNext"
OR
runtime works fine, producing
class MyArray extends Array {
foo;
constructor(foo){
var _temp;
return !foo ? [] : (_temp = super(), this.foo = foo, _temp);
}
}
new MyArray();
same as SWC does
Side note
SWC compiler can compile this just fine (as (_temp = super(), this.foo = foo, _temp)),
so this code has runtime errors only when compiled with tsc.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Stackblitz reproduction in index.ts and compare compilation with target set to ESNext versus when it is omitted in tsconfig.json. Trace the compiler path that reports TS2401 for the derived constructor; done means the invalid case is diagnosed consistently or the emitted ESNext-target code runs without the reported runtime error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100