Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic option to control return value for .json and .parse #184

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/entrypoints/fetch.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
interface Body {
json(): Promise<unknown>;
json<T = unknown>(): Promise<T>;
}
4 changes: 2 additions & 2 deletions src/entrypoints/json-parse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ interface JSON {
* @param reviver A function that transforms the results. This function is called for each member of the object.
* If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
parse(
parse<T = unknown>(
text: string,
reviver?: (this: any, key: string, value: any) => any,
): unknown;
): T;
}
7 changes: 4 additions & 3 deletions src/tests/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ doNotExecute(async () => {
});

doNotExecute(async () => {
// Make tests fail when someone tries to PR res.json<T>
type Named = { name: string };

const result = await fetch("/").then((res) => {
// @ts-expect-error
return res.json<string>();
return res.json<Named>();
});

type tests = [Expect<Equal<typeof result, Named>>];
});
7 changes: 4 additions & 3 deletions src/tests/json-parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ doNotExecute(() => {
});

doNotExecute(() => {
// Make tests fail when someone tries to PR JSON.parse<T>
type Named = { name: string };

// @ts-expect-error
const result = JSON.parse<string>("{}");
const result = JSON.parse<Named>("{ name: 'ts-reset' }");

type tests = [Expect<Equal<typeof result, Named>>];
});