Skip to content

Commit f4d8221

Browse files
committed
✨ Add strong type definition for fetch().json()
Closes uhyo#24
1 parent 1507792 commit f4d8221

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

docs/diff/dom.generated.d.ts.md

+11
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,16 @@ Index: dom.generated.d.ts
1111
/// Window APIs
1212
/////////////////////////////
1313

14+
@@ -2800,9 +2801,9 @@
15+
readonly bodyUsed: boolean;
16+
arrayBuffer(): Promise<ArrayBuffer>;
17+
blob(): Promise<Blob>;
18+
formData(): Promise<FormData>;
19+
- json(): Promise<any>;
20+
+ json(): Promise<JSONValue>;
21+
text(): Promise<string>;
22+
}
23+
24+
interface BroadcastChannelEventMap {
1425

1526
```

generated/lib.dom.d.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -2802,9 +2802,16 @@ interface Body {
28022802
arrayBuffer(): Promise<ArrayBuffer>;
28032803
blob(): Promise<Blob>;
28042804
formData(): Promise<FormData>;
2805-
json(): Promise<any>;
2805+
json(): Promise<JSONValue>;
28062806
text(): Promise<string>;
28072807
}
2808+
// readonly body: ReadableStream<Uint8Array> | null;
2809+
// readonly bodyUsed: boolean;
2810+
// arrayBuffer(): Promise<ArrayBuffer>;
2811+
// blob(): Promise<Blob>;
2812+
// formData(): Promise<FormData>;
2813+
// json(): Promise<any>;
2814+
// text(): Promise<string>;
28082815

28092816
interface BroadcastChannelEventMap {
28102817
message: MessageEvent;

lib/lib.dom.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
interface Body {
2+
readonly body: ReadableStream<Uint8Array> | null;
3+
readonly bodyUsed: boolean;
4+
arrayBuffer(): Promise<ArrayBuffer>;
5+
blob(): Promise<Blob>;
6+
formData(): Promise<FormData>;
7+
json(): Promise<JSONValue>;
8+
text(): Promise<string>;
9+
}

tests/src/dom.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { expectType } from "tsd";
2+
3+
const test = async (url: string) => {
4+
const response = await fetch(url);
5+
const json = await response.json();
6+
expectType<JSONValue>(json);
7+
};

tests/tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"compilerOptions": {
33
"target": "esnext",
4-
"lib": ["esnext"],
4+
"lib": ["esnext", "dom"],
55
"module": "esnext",
66
"moduleResolution": "node",
77
"noEmit": true,
88
"strict": true,
99
"types": []
1010
},
1111
"include": ["./src/*.ts"]
12-
}
12+
}

0 commit comments

Comments
 (0)