microsoft / microsoft/TypeScript

`Error.stack` type definition in lib.es5.d.ts is incorrect / `exactOptionalPropertyTypes` incompatible

Aperta
#45,748 7 commenti 4 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: lib.d.ts
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

🔎 Search Terms

Error.stack, exactOptionalPropertyTypes, ErrnoException

🕗 Version & Regression Information
  • This is a type issue
  • This changed in #8705 but it took until the 4.4 release for it to surface, thanks to exactOptionalPropertyTypes and it making Error incompatible with a corrected Error-subclass in @types/node: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/55425
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about es5
⏯ Playground Link

Playground link with relevant code which is valid according to the ECMAScript spec and basically every other JS spec out there as .stack isn't standardized (as far as eg. MDN knows at least)

Edit: @ljharb has an ECMAScript Stage 1 proposal for an Error Stacks spec.

💻 Code
interface MyCoolStack {
    line: string
}

interface MyError extends Error {
    stack: MyCoolStack
}
🙁 Actual behavior

Got error:

Interface 'MyError' incorrectly extends interface 'Error'.
  Types of property 'stack' are incompatible.
    Type 'MyCoolStack | undefined' is not assignable to type 'string'.
      Type 'undefined' is not assignable to type 'string'.
🙂 Expected behavior

One of two:

  1. Either that it passes completely...
  2. ...or at least that it complains the same as it does with exactOptionalPropertyTypes turned off:
Interface 'MyError' incorrectly extends interface 'Error'.
  Types of property 'stack' are incompatible.
    Type 'MyCoolStack | undefined' is not assignable to type 'string | undefined'.
      Type 'MyCoolStack' is not assignable to type 'string'.

Edit: The ECMAScript Stage 1 proposal for an Error Stacks spec defines that the stack should be of type string|undefined, which makes the current type of stack?:string wrong and should be replaced with stack?:string|undefined if one agrees with that specs view of the current state of Error stacks.

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

Esamina la dichiarazione di Error in lib.es5.d.ts e riproduci l’incompatibilità segnalata usando il TypeScript Playground collegato, con exactOptionalPropertyTypes abilitato e disabilitato. Confronta i diagnostici risultanti con il comportamento previsto e la proposta Error Stacks collegata; il lavoro è completato quando la dichiarazione riflette la semantica scelta dal progetto per Error.stack.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.