Skip to content

Commit

Permalink
Support custom Error subclass in the TypeScript types (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
gr2m and sindresorhus committed Aug 21, 2020
1 parent 58a3c04 commit 8ba26ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
Create an error from multiple errors.
*/
declare class AggregateError extends Error implements Iterable<Error> {
declare class AggregateError<T extends Error = Error> extends Error implements Iterable<T> {
readonly name: 'AggregateError';

/**
Expand Down Expand Up @@ -43,9 +43,9 @@ declare class AggregateError extends Error implements Iterable<Error> {
//=> [Error: baz]
```
*/
constructor(errors: ReadonlyArray<Error | {[key: string]: any} | string>);
constructor(errors: ReadonlyArray<T | {[key: string]: any} | string>);

[Symbol.iterator](): IterableIterator<Error>;
[Symbol.iterator](): IterableIterator<T>;
}

export = AggregateError;
17 changes: 17 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ expectType<IterableIterator<Error>>(aggregateError[Symbol.iterator]());
for (const error of aggregateError) {
expectType<Error>(error);
}

class CustomError extends Error {
public foo: string;

constructor(message: string) {
super(message)
this.name = 'CustomError';
this.foo = 'bar';
}
}
const customAggregateError = new AggregateError<CustomError>([
new CustomError('foo')
]);

for (const error of customAggregateError) {
expectType<string>(error.foo);
}

0 comments on commit 8ba26ec

Please sign in to comment.