microsoft / microsoft/TypeScript

TS2401 does not work with `"target": "ESNext"`

Aperta
#54,445 5 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: classes Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla riproduzione Stackblitz collegata in index.ts e confronta la compilazione con target impostato su ESNext con quella in cui viene omesso in tsconfig.json. Traccia il percorso del compilatore che segnala TS2401 per il costruttore derivato; il lavoro è completato quando il caso non valido viene diagnosticato in modo coerente oppure il codice emesso con target ESNext viene eseguito senza l’errore di runtime segnalato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.