Skip to content

Commit

Permalink
fix: recursively simplify returned objects (#1847)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Oct 21, 2023
1 parent 3611071 commit e37359c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/types/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ type SerializeObject<T extends object> = {
*/
export type Simplify<TType> = TType extends any[] | Date
? TType
: { [K in keyof TType]: TType[K] };
: { [K in keyof TType]: Simplify<TType[K]> };
25 changes: 25 additions & 0 deletions test/fixture/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, it } from "vitest";
import { EventHandler, EventHandlerRequest, defineEventHandler } from "h3";
import { $Fetch } from "../..";
import { defineNitroConfig } from "../../src/config";
import { Serialize, Simplify } from "../../src/types";

interface TestResponse {
message: string;
Expand Down Expand Up @@ -307,3 +308,27 @@ describe("defineCachedEventHandler", () => {
>();
});
});

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

it("Simplify", () => {
expectTypeOf<Simplify<Serialize<{ test: Date }>>>().toEqualTypeOf<{
test: string;
}>();
expectTypeOf<
Simplify<Serialize<{ test: Map<string, string> }>>
>().toEqualTypeOf<{ test: Record<string, never> }>();
expectTypeOf<
Simplify<Serialize<{ nested: { test: Map<string, string> } }>>
>().toEqualTypeOf<{ nested: { test: Record<string, never> } }>();
});
});

0 comments on commit e37359c

Please sign in to comment.