microsoft / microsoft/TypeScript

Make AggregateError generic to represent error types

Open
#54,063 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the AggregateError declarations in lib.es2021.promise.d.ts and lib.es2022.error.d.ts, comparing the existing constructor overloads and errors property with the proposed generic forms. Verify that the default preserves existing usage, inferred errors types are represented, and both declarations remain consistent.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.