Skip to content

Commit

Permalink
Fix return type for unknown input (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jan 4, 2025
1 parent e593d37 commit 64ce883
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 3 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ serializeError(error);
*/
export function serializeError<ErrorType>(error: ErrorType, options?: Options): ErrorType extends Primitive
? ErrorType
: ErrorObject;
: unknown extends ErrorType
? unknown
: ErrorObject;

/**
Deserialize a plain object or any value into an `Error` object.
Expand Down
13 changes: 7 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType, expectAssignable} from 'tsd';
import {expectTypeOf} from 'expect-type';
import {
serializeError,
deserializeError,
Expand All @@ -9,16 +9,17 @@ import {

const error = new Error('unicorn');

expectType<number>(serializeError(1));
expectType<ErrorObject>(serializeError(error));
expectAssignable<Options>({maxDepth: 1});
expectTypeOf(serializeError(1)).toEqualTypeOf<number>();
expectTypeOf(serializeError(error as unknown)).toEqualTypeOf<unknown>();
expectTypeOf(serializeError(error)).toEqualTypeOf<ErrorObject>();
expectTypeOf({maxDepth: 1}).toMatchTypeOf<Options>();

expectType<Error>(deserializeError({
expectTypeOf(deserializeError({
message: 'error message',
stack: 'at <anonymous>:1:13',
name: 'name',
code: 'code',
}));
})).toEqualTypeOf<Error>();

addKnownErrorConstructor(Error);

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"devDependencies": {
"ava": "^6.2.0",
"expect-type": "^1.1.0",
"tsd": "^0.31.2",
"xo": "^0.60.0"
}
Expand Down

0 comments on commit 64ce883

Please sign in to comment.