mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-05 15:27:05 +00:00
Allow Body.json() typed result
This commit is contained in:
Vendored
+1
-1
@@ -34,7 +34,7 @@ interface Body {
|
||||
arrayBuffer(): Promise<ArrayBuffer>;
|
||||
blob(): Promise<Blob>;
|
||||
formData(): Promise<FormData>;
|
||||
json(): Promise<any>;
|
||||
json<T>(): Promise<T>;
|
||||
text(): Promise<string>;
|
||||
}
|
||||
|
||||
|
||||
@@ -68,4 +68,40 @@ function handlePromise(promise: Promise<Response>) {
|
||||
}).then((text) => {
|
||||
console.log(text);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function test_Body_json() {
|
||||
interface FooBar {
|
||||
foo: string;
|
||||
bar: string;
|
||||
}
|
||||
|
||||
fetch('http://test.com')
|
||||
.then(response => response.json<FooBar>())
|
||||
.then(fooBar => {
|
||||
console.log(fooBar.foo);
|
||||
console.log(fooBar.bar);
|
||||
});
|
||||
|
||||
fetch('http://test.com')
|
||||
.then(response => <Promise<FooBar>> response.json())
|
||||
.then(fooBar => {
|
||||
console.log(fooBar.foo);
|
||||
console.log(fooBar.bar);
|
||||
});
|
||||
|
||||
fetch('http://test.com')
|
||||
.then(response => response.json() as Promise<FooBar>)
|
||||
.then(fooBar => {
|
||||
console.log(fooBar.foo);
|
||||
console.log(fooBar.bar);
|
||||
});
|
||||
|
||||
fetch('http://test.com')
|
||||
.then(response => response.json())
|
||||
.then(fooBar => {
|
||||
// fooBar is {}, not FooBar
|
||||
//console.log(fooBar.foo);
|
||||
//console.log(fooBar.bar);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user