microsoft / microsoft/TypeScript
TS2401 does not work with `"target": "ESNext"`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der verknüpften Stackblitz-Reproduktion in index.ts und vergleiche die Kompilierung mit target auf ESNext gesetzt mit der Kompilierung, wenn es in tsconfig.json weggelassen wird. Verfolge den Compilerpfad, der TS2401 für den abgeleiteten Konstruktor meldet; abgeschlossen ist die Aufgabe, wenn der ungültige Fall konsistent diagnostiziert wird oder der erzeugte Code mit ESNext als target ohne den gemeldeten Laufzeitfehler ausgeführt wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 35/100