Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 2, 2019
1 parent 30dfdfc commit 9212299
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
63 changes: 39 additions & 24 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
import {Primitive, JsonObject} from 'type-fest';

export type ErrorObject = {
name?: string;
stack?: string;
message?: string;
code?: string;
} & JsonObject;

/**
Serialize an error into a plain object.
@example
```
import serializeError from 'serialize-error';
const error = new Error('🦄');
console.log(error);
//=> [Error: 🦄]
console.log(serializeError(error));
//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'}
```
*/
export default function serializeError<ErrorType>(error: ErrorType): ErrorType extends Primitive ? ErrorType : ErrorObject;
declare namespace serializeError {
type ErrorObject = {
name?: string;
stack?: string;
message?: string;
code?: string;
} & JsonObject;
}

declare const serializeError: {
/**
Serialize an error into a plain object.
@example
```
import serializeError = require('serialize-error');
const error = new Error('🦄');
console.log(error);
//=> [Error: 🦄]
console.log(serializeError(error));
//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'}
```
*/
<ErrorType>(error: ErrorType): ErrorType extends Primitive
? ErrorType
: serializeError.ErrorObject;

// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function serializeError<ErrorType>(
// error: ErrorType
// ): ErrorType extends Primitive ? ErrorType : ErrorObject;
// export = serializeError;
default: typeof serializeError;
};

export = serializeError;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ const serializeError = value => {
};

module.exports = serializeError;
// TODO: Remove this for the next major release
module.exports.default = serializeError;
5 changes: 3 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expectType} from 'tsd-check';
import serializeError, {ErrorObject} from '.';
import {expectType} from 'tsd';
import serializeError = require('.');
import {ErrorObject} from '.';

const error = new Error('unicorn');

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -32,8 +32,8 @@
"type-fest": "^0.3.0"
},
"devDependencies": {
"ava": "^1.3.1",
"tsd-check": "^0.5.0",
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
}
}

0 comments on commit 9212299

Please sign in to comment.