microsoft / microsoft/TypeScript

Make AggregateError generic to represent error types

Ouverte
#54,063 9 commentaires 2 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par les déclarations de AggregateError dans lib.es2021.promise.d.ts et lib.es2022.error.d.ts, en comparant les surcharges de constructeur existantes et la propriété errors avec les formes génériques proposées. Vérifiez que la valeur par défaut préserve l’utilisation existante, que les types errors inférés soient représentés et que les deux déclarations restent cohérentes.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
tooling
Type d'issue
Fonctionnalité
Difficulté
2/5
Temps estimé
1-3 heures
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
48/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.