microsoft / microsoft/TypeScript
All types of ECMAScript NativeErrors are always subtype reduced between themselves
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
⚙ Compilation target
ESNext
⚙ Library
es5
Missing / Incorrect Definition
All error instance types defined in es5 are equivalent in eyes of TypeScript, but are distinguishable at runtime with instanceof.
Here is how each error is defined right now:
interface XError extends Error { // <- Same as Error!
}
interface XErrorConstructor extends ErrorConstructor {
new (message?: string): XError
(message?: string): XError
readonly prototype: XError
}
declare var XError: XErrorConstructor
As you can see, any error is structurally equivalent to another, including Error itself. This makes the issue propagate to error constructors as well.
On a somewhat related note, child classes of Error (class MyError extends Error {}) do not have this issue.
This affects libraries that attempt making error-handling typed (for example, Effect.ts, NeverThrow 🙅, etc.), making them unreliable when using built-in errors. (Which are, arguably, the most commonly used type of errors.)
Sample Code
// Should be (SyntaxError | TypeError)[]
const twoErrors0 /* <? SyntaxError[] */ = [
new SyntaxError(),
new TypeError(),
]
// Should be (TypeError | SyntaxError)[]
const twoErrors1 /* <? TypeError[] */ = [
new TypeError(),
new SyntaxError(),
]
// --------------------------------------------------------------------
// Should return RangeError | URIError
function makeError0(flag: boolean) /* : RangeError is inferred */ {
if (flag)
return new RangeError()
return new URIError()
}
// Should return URIError | RangeError
function makeError1(flag: boolean) /* : URIError is inferred */ {
if (flag)
return new URIError()
return new RangeError()
}
const madeError = makeError1(true)
if (madeError instanceof URIError) {
// handle URIError
} else {
console.log(madeError)
// ^? madeError: never
// Programmer thinks they handled all cases, because the error is of type never here.
// madeError is actually an instance of RangeError at runtime.
// This happened due to TypeScript wrongly inferring makeError1's return type.
}
Documentation Link
ECMAScript 5.1: 15.11.7 NativeError Object Structure
ECMAScript 5.1: 15.3.5.3 [[HasInstance]] (V)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con le dichiarazioni ES5 NativeError e i casi di esempio di inferenza dei tipi nell’issue. Verifica in che modo le dichiarazioni attuali rendono i tipi di errore integrati strutturalmente equivalenti e in che modo la riduzione dei sottotipi gestisce i relativi costruttori. Il lavoro è completato quando i tipi di errore nativi rimangono distinguibili negli esempi mostrati di array e tipi restituiti, con copertura di regressione per questi casi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100