Skip to content

Commit 41c9c24

Browse files
BendingBendersindresorhus
authored andcommitted
Add TypeScript definition (#8)
1 parent 00dd2dc commit 41c9c24

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

index.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Create an error from multiple errors.
3+
*/
4+
export default class AggregateError extends Error implements Iterable<Error> {
5+
readonly name: 'AggregateError';
6+
7+
/**
8+
* @param errors - If a string, a new `Error` is created with the string as the error message. If a non-Error object, a new `Error` is created with all properties from the object copied over.
9+
* @returns An Error that is also an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Iterables) for the individual errors.
10+
*/
11+
constructor(errors: ReadonlyArray<Error | {[key: string]: unknown} | string>);
12+
13+
[Symbol.iterator](): IterableIterator<Error>;
14+
}

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ class AggregateError extends Error {
4545
}
4646

4747
module.exports = AggregateError;
48+
module.exports.default = AggregateError;

index.test-d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {expectType} from 'tsd-check';
2+
import AggregateError from '.';
3+
4+
const aggregateError = new AggregateError([
5+
new Error('foo'),
6+
{foo: 'bar'},
7+
'bar'
8+
]);
9+
expectType<Iterable<Error>>(aggregateError);
10+
expectType<IterableIterator<Error>>(aggregateError[Symbol.iterator]());
11+
12+
for (const error of aggregateError) {
13+
expectType<Error>(error);
14+
}

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd-check"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"aggregate",
@@ -35,6 +36,7 @@
3536
},
3637
"devDependencies": {
3738
"ava": "^1.2.1",
39+
"tsd-check": "^0.3.0",
3840
"xo": "^0.24.0"
3941
}
4042
}

0 commit comments

Comments
 (0)