microsoft / microsoft/TypeScript

All types of ECMAScript NativeErrors are always subtype reduced between themselves

Offen
#62,134 6 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

⚙ 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)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit den ES5-NativeError-Deklarationen und den Beispiel-Fällen zur Typinferenz im Issue. Prüfen Sie, wie die aktuellen Deklarationen dafür sorgen, dass die integrierten Fehlertypen strukturell äquivalent sind, und wie die Subtyp-Reduktion ihre Konstruktoren behandelt. Erledigt ist die Aufgabe, wenn die nativen Fehlertypen in den gezeigten Array- und Rückgabetyp-Beispielen unterscheidbar bleiben und diese Fälle durch Regressionstests abgedeckt sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.