TLDR:
Is this type possible somehow in TS? Exclude<number, 200 | 400>
("any number except 200 or 400")
I have the following use case. I have a response type, which is generic:
type HttpResponse<Body = any, StatusCode = number> = {
body: Body
statusCode: StatusCode
}
And I'd like to use the status code as a discriminator:
// Succes
type SuccessResponse = HttpResponse<SomeType, 200>
// Known error
type ClientErrorResponse = HttpResponse<ClientError, 400>
// Anything else, generic error, issue is with the status code here.
type OtherErrorResponse = HttpResponse<GenericError, Exclude<number, 200 | 400>>
// The response type is a union of the above
type MyResponse = SuccessResponse | ClientErrorResponse | OtherErrorResponse
When I use the MyResponse
type, I'd like to use the status code as a discriminator, eg.:
const response: MyResponse = ...
if(response.statusCode === 200) {
// response is inferred as SuccessResponse => body is inferred as SomeType
} else if(response.statusCode === 400) {
// response is inferred as ClientErrorResponse => body is inferred as ClientError
} else {
// response is inferred as OtherErrorResponse => body is inferred as GenericError
}
However it doesn't work like this as Exclude<number, 200 | 400>
is the same as just number
. How do I solve this? Is the type "any number except 200 or 400"
possible with typescript? Any other creative solutions?
number
to be an infinite set (as the compiler currently does). – Ammieammine