microsoft / microsoft/TypeScript

Make AggregateError generic to represent error types

Offen
#54,063 9 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

lib Update Request

Make it possible for an AggregateError to carry a type parameter representing the shape of the items in its errors array, while preserving backwards compatibility using type parameter defaults.

Specifically, the current definition is:

// In lib.es2021.promise.d.ts
interface AggregateError extends Error {
    errors: any[]
}

interface AggregateErrorConstructor {
    new(errors: Iterable<any>, message?: string): AggregateError;
    (errors: Iterable<any>, message?: string): AggregateError;
    readonly prototype: AggregateError;
}

// In lib.es2022.error.d.ts; adds `options` argument.
interface AggregateErrorConstructor {
    new (
        errors: Iterable<any>,
        message?: string,
        options?: ErrorOptions
    ): AggregateError;
    (
        errors: Iterable<any>,
        message?: string,
        options?: ErrorOptions
    ): AggregateError;
}

I'm proposing instead:

// In lib.es2021.promise.d.ts
interface AggregateError<T = any> extends Error {
    errors: T[]
}

interface AggregateErrorConstructor {
    new<T = any>(errors: Iterable<T>, message?: string): AggregateError<T>;
    <T = any>(errors: Iterable<T>, message?: string): AggregateError<T>;
    readonly prototype: AggregateError;
}

// In lib.es2022.error.d.ts
interface AggregateErrorConstructor {
    new<T = any>(errors: Iterable<T>, message?: string,
        options?: ErrorOptions): AggregateError<T>;
    <T = any>(errors: Iterable<T>, message?: string,
        options?: ErrorOptions): AggregateError<T>;
    readonly prototype: AggregateError;
}

Again, the only difference is the addition of a new T parameter defaulting to any (the old value), with the errors key updated from any[] to T[].

Configuration Check

My compilation target is ES2022 and my lib is ["ES2022", "DOM"].

Missing / Incorrect Definition

NA, as the property isn't missing; it's just underspecified.

I think this would be a nice quality of life improvement, esp for code that returns AggregateErrors (sorta analogous to the functional style of returning a Result type) rather than throwing them.

Sample Code

// Current
const x = new AggregateError([new Error("Something something...")]).errors; // type is any[]

// With proposal
const x = new AggregateError([new Error("Something something...")]).errors // type is Error[]

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError/errors

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 AggregateError-Deklarationen in lib.es2021.promise.d.ts und lib.es2022.error.d.ts und vergleichen Sie die vorhandenen Konstruktorüberladungen und die errors-Eigenschaft mit den vorgeschlagenen generischen Formen. Überprüfen Sie, dass der Standardwert die bestehende Verwendung beibehält, dass inferierte errors-Typen dargestellt werden und dass beide Deklarationen konsistent bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
48/100

Neue Issues direkt in Ihr Postfach

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