Skip to content
Permalink
Browse files

Make the `.json()` method generic (#98)

It can still be used as before without a generic.

Fixes #80
  • Loading branch information...
sindresorhus committed Feb 4, 2019
1 parent 8f17a13 commit ce12902195558b84d4c22b0676526153be334328
Showing with 27 additions and 1 deletion.
  1. +22 −1 index.d.ts
  2. +5 −0 index.test-d.ts
@@ -81,9 +81,30 @@ export interface Options extends RequestInit {
*/
export interface ResponsePromise extends Promise<Response> {
arrayBuffer(): Promise<ArrayBuffer>;

blob(): Promise<Blob>;

formData(): Promise<FormData>;
json(): Promise<JSONValue>;

// TODO: Use `json<T extends JSONValue>(): Promise<T>;` when it's fixed in TS.
// See https://github.com/sindresorhus/ky/pull/80
/**
* Get the response body as JSON.
*
* @example
*
* const json = await ky(…).json();
*
* @example
*
* interface Result {
* value: number;
* }
* const result = await ky(…).json<Result>();
*/
json<T = JSONValue>(): Promise<T>;

text(): Promise<string>;
}

@@ -68,3 +68,8 @@ ky.post(server.url, {
});

expectType<JSONValue>(await ky(server.url).json());

interface Result {
value: number;
}
expectType<Result>(await ky(server.url).json<Result>());

0 comments on commit ce12902

Please sign in to comment.
You can’t perform that action at this time.