Skip to content

Commit 8ba26ec

Browse files
gr2msindresorhus
andauthored
Support custom Error subclass in the TypeScript types (#13)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 58a3c04 commit 8ba26ec

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

index.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
Create an error from multiple errors.
33
*/
4-
declare class AggregateError extends Error implements Iterable<Error> {
4+
declare class AggregateError<T extends Error = Error> extends Error implements Iterable<T> {
55
readonly name: 'AggregateError';
66

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

48-
[Symbol.iterator](): IterableIterator<Error>;
48+
[Symbol.iterator](): IterableIterator<T>;
4949
}
5050

5151
export = AggregateError;

index.test-d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,20 @@ expectType<IterableIterator<Error>>(aggregateError[Symbol.iterator]());
1212
for (const error of aggregateError) {
1313
expectType<Error>(error);
1414
}
15+
16+
class CustomError extends Error {
17+
public foo: string;
18+
19+
constructor(message: string) {
20+
super(message)
21+
this.name = 'CustomError';
22+
this.foo = 'bar';
23+
}
24+
}
25+
const customAggregateError = new AggregateError<CustomError>([
26+
new CustomError('foo')
27+
]);
28+
29+
for (const error of customAggregateError) {
30+
expectType<string>(error.foo);
31+
}

0 commit comments

Comments
 (0)