TS2401 does not work with `"target": "ESNext"`
#54,445 opened on 2023幎5æ29æ¥
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
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.