-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor TypeScript definition to CommonJS compatible export (#15)
- Loading branch information
1 parent
30dfdfc
commit 9212299
Showing
4 changed files
with
46 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters