Skip to content

Commit

Permalink
fix(types): return T from Serialize when it extends undefined (#…
Browse files Browse the repository at this point in the history
…2286)

Co-authored-by: Daniel Roe <[email protected]>
  • Loading branch information
DamianGlowala and danielroe committed Mar 21, 2024
1 parent 5184e45 commit a077cb2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/types/serialize.ts
Expand Up @@ -28,7 +28,7 @@ type FilterKeys<TObj extends object, TFilter> = {
// prettier-ignore
export type Serialize<T> =
IsAny<T> extends true ? any :
T extends JsonPrimitive ? T :
T extends JsonPrimitive | undefined ? T :
T extends Map<any,any> | Set<any> ? Record<string, never> :
T extends NonJsonPrimitive ? never :
T extends { toJSON(): infer U } ? U :
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/types.ts
Expand Up @@ -311,7 +311,14 @@ describe("defineCachedEventHandler", () => {

describe("type helpers", () => {
it("Serialize", () => {
expectTypeOf<Serialize<undefined>>().toEqualTypeOf<undefined>();
expectTypeOf<Serialize<{ test?: string }>>().toEqualTypeOf<{
test?: string;
}>();
expectTypeOf<Serialize<{ test: Date }>>().toEqualTypeOf<{ test: string }>();
expectTypeOf<Serialize<{ test?: Date }>>().toEqualTypeOf<{
test?: string;
}>();
expectTypeOf<Serialize<{ test: Map<string, string> }>>().toEqualTypeOf<{
test: Record<string, never>;
}>();
Expand Down

0 comments on commit a077cb2

Please sign in to comment.