Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/14-generics.solution.2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// CODE

import { it } from "vitest";
import { z } from "zod";
import { Equal, Expect } from "./helpers/type-utils";

const genericFetch = <ZSchema>(
url: string,
schema: z.ZodSchema<ZSchema>,
) => {
return fetch(url)
.then((res) => res.json())
.then((result) => schema.parse(result));
};

// TESTS

it("Should fetch from the Star Wars API", async () => {
const result = await genericFetch(
"https://swapi.dev/api/people/1",
z.object({
name: z.string(),
}),
);

type cases = [
// Result should equal { name: string }, not any
Expect<Equal<typeof result, { name: string }>>,
];
});