Skip to content

Commit

Permalink
Add TimeoutError and HTTPError to ky TypeScript namespace (#241)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
hasparus and sindresorhus committed Mar 6, 2020
1 parent d547e89 commit 9c9dfe9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,9 @@ declare const ky: {
readonly HTTPError: typeof HTTPError;
};

declare namespace ky {
export type TimeoutError = InstanceType<typeof ky.TimeoutError>;
export type HTTPError = InstanceType<typeof ky.HTTPError>;
}

export default ky;
14 changes: 14 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,17 @@ ky(url, {
afterStatusCodes: []
}
});

try {
await ky.get(url);
} catch (error) {
if (error instanceof ky.HTTPError) {
expectType<ky.HTTPError>(error);
const {status} = error.response;
expectType<number>(status);
} else if (error instanceof ky.TimeoutError) {
expectType<ky.TimeoutError>(error);
} else {
throw error;
}
}

0 comments on commit 9c9dfe9

Please sign in to comment.