Skip to content

Commit a077cb2

Browse files
fix(types): return T from Serialize when it extends undefined (#2286)
Co-authored-by: Daniel Roe <[email protected]>
1 parent 5184e45 commit a077cb2

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/types/serialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type FilterKeys<TObj extends object, TFilter> = {
2828
// prettier-ignore
2929
export type Serialize<T> =
3030
IsAny<T> extends true ? any :
31-
T extends JsonPrimitive ? T :
31+
T extends JsonPrimitive | undefined ? T :
3232
T extends Map<any,any> | Set<any> ? Record<string, never> :
3333
T extends NonJsonPrimitive ? never :
3434
T extends { toJSON(): infer U } ? U :

test/fixture/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,14 @@ describe("defineCachedEventHandler", () => {
311311

312312
describe("type helpers", () => {
313313
it("Serialize", () => {
314+
expectTypeOf<Serialize<undefined>>().toEqualTypeOf<undefined>();
315+
expectTypeOf<Serialize<{ test?: string }>>().toEqualTypeOf<{
316+
test?: string;
317+
}>();
314318
expectTypeOf<Serialize<{ test: Date }>>().toEqualTypeOf<{ test: string }>();
319+
expectTypeOf<Serialize<{ test?: Date }>>().toEqualTypeOf<{
320+
test?: string;
321+
}>();
315322
expectTypeOf<Serialize<{ test: Map<string, string> }>>().toEqualTypeOf<{
316323
test: Record<string, never>;
317324
}>();

0 commit comments

Comments
 (0)