Allow Body.json() typed result

This commit is contained in:
Tanguy Krotoff
2016-11-19 12:56:37 +01:00
parent a666820ef2
commit fd1bbbf367
2 changed files with 38 additions and 2 deletions
+1 -1
View File
@@ -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>;
}
+37 -1
View File
@@ -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);
});
}