prettymuchbryce / http-status-codes

Constants enumerating the HTTP status codes. All status codes defined in RFC1945 (HTTP/1.0, RFC2616 (HTTP/1.1), and RFC2518 (WebDAV) are supported.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript: export union of all codes

OliverJAsh opened this issue · comments

In TypeScript it would be useful to have a union type that represents all possible status codes.

This is possible in user land by doing this:

import HttpStatusCodes from 'http-status-codes';

type HttpStatusCodesMap = typeof HttpStatusCodes;
export type HttpStatusCode = Extract<HttpStatusCodesMap[keyof HttpStatusCodesMap], number>;

However it would be nice if the official type definitions provided this out of the box, as I'm fairly sure it's a common requirement. WDYT?

I'm still not able to get this to work.

I want to define a custom type in my d.ts. However, if I define my type as HttpStatusCodes, compiler says it can´t find the name.

If I add it as a number, everything works fine, so for that, I need a type that is the union of all error codes (number). How do I accomplish that?

Thanks!