Skip to content

Commit fbf40b7

Browse files
BendingBendersindresorhus
authored andcommitted
Refactor TypeScript definition to CommonJS compatible export (#9)
1 parent 9a6e845 commit fbf40b7

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

index.d.ts

+43-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
11
/**
2-
* Create an error from multiple errors.
3-
*/
4-
export default class AggregateError extends Error implements Iterable<Error> {
2+
Create an error from multiple errors.
3+
*/
4+
declare class AggregateError extends Error implements Iterable<Error> {
55
readonly name: 'AggregateError';
66

77
/**
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-
*/
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+
@example
12+
```
13+
import AggregateError = require('aggregate-error');
14+
15+
const error = new AggregateError([new Error('foo'), 'bar', {message: 'baz'}]);
16+
17+
throw error;
18+
19+
// AggregateError:
20+
// Error: foo
21+
// at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:33)
22+
// Error: bar
23+
// at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
24+
// Error: baz
25+
// at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
26+
// at AggregateError (/Users/sindresorhus/dev/aggregate-error/index.js:19:3)
27+
// at Object.<anonymous> (/Users/sindresorhus/dev/aggregate-error/example.js:3:13)
28+
// at Module._compile (module.js:556:32)
29+
// at Object.Module._extensions..js (module.js:565:10)
30+
// at Module.load (module.js:473:32)
31+
// at tryModuleLoad (module.js:432:12)
32+
// at Function.Module._load (module.js:424:3)
33+
// at Module.runMain (module.js:590:10)
34+
// at run (bootstrap_node.js:394:7)
35+
// at startup (bootstrap_node.js:149:9)
36+
37+
38+
for (const individualError of error) {
39+
console.log(individualError);
40+
}
41+
//=> [Error: foo]
42+
//=> [Error: bar]
43+
//=> [Error: baz]
44+
```
45+
*/
1146
constructor(errors: ReadonlyArray<Error | {[key: string]: unknown} | string>);
1247

1348
[Symbol.iterator](): IterableIterator<Error>;
1449
}
50+
51+
export = AggregateError;

index.js

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

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

index.test-d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {expectType} from 'tsd-check';
2-
import AggregateError from '.';
1+
import {expectType} from 'tsd';
2+
import AggregateError = require('.');
33

44
const aggregateError = new AggregateError([
55
new Error('foo'),

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && ava && tsd-check"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
1919
"index.js",
@@ -32,11 +32,11 @@
3232
],
3333
"dependencies": {
3434
"clean-stack": "^2.0.0",
35-
"indent-string": "^3.0.0"
35+
"indent-string": "^3.2.0"
3636
},
3737
"devDependencies": {
38-
"ava": "^1.2.1",
39-
"tsd-check": "^0.3.0",
38+
"ava": "^1.4.1",
39+
"tsd": "^0.7.1",
4040
"xo": "^0.24.0"
4141
}
4242
}

0 commit comments

Comments
 (0)