From fd1bbbf367d3d01a7f3c1d87408a7c765721daa4 Mon Sep 17 00:00:00 2001 From: Tanguy Krotoff Date: Sat, 19 Nov 2016 12:56:37 +0100 Subject: [PATCH] Allow Body.json() typed result --- whatwg-fetch/index.d.ts | 2 +- whatwg-fetch/whatwg-fetch-tests.ts | 38 +++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/whatwg-fetch/index.d.ts b/whatwg-fetch/index.d.ts index f17d88a7df..c3667ae402 100644 --- a/whatwg-fetch/index.d.ts +++ b/whatwg-fetch/index.d.ts @@ -34,7 +34,7 @@ interface Body { arrayBuffer(): Promise; blob(): Promise; formData(): Promise; - json(): Promise; + json(): Promise; text(): Promise; } diff --git a/whatwg-fetch/whatwg-fetch-tests.ts b/whatwg-fetch/whatwg-fetch-tests.ts index aa6b90b8c8..2ad0f1ce43 100644 --- a/whatwg-fetch/whatwg-fetch-tests.ts +++ b/whatwg-fetch/whatwg-fetch-tests.ts @@ -68,4 +68,40 @@ function handlePromise(promise: Promise) { }).then((text) => { console.log(text); }); -} \ No newline at end of file +} + +function test_Body_json() { + interface FooBar { + foo: string; + bar: string; + } + + fetch('http://test.com') + .then(response => response.json()) + .then(fooBar => { + console.log(fooBar.foo); + console.log(fooBar.bar); + }); + + fetch('http://test.com') + .then(response => > response.json()) + .then(fooBar => { + console.log(fooBar.foo); + console.log(fooBar.bar); + }); + + fetch('http://test.com') + .then(response => response.json() as Promise) + .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); + }); +}