问题描述
TLDR:
这种类型在 TS 中是否可行? Exclude<number,200 | 400>
(“除 200 或 400 之外的任何数字”)
type HttpResponse<Body = any,StatusCode = number> = {
body: Body
statusCode: StatusCode
}
我想使用状态代码作为鉴别器:
// 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
当我使用 MyResponse
类型时,我想使用状态代码作为鉴别器,例如:
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
}
但是它不是这样工作的,因为 Exclude<number,200 | 400>
与 number
相同。我该如何解决这个问题?打字稿可以使用 "any number except 200 or 400"
类型吗?还有其他创造性的解决方案吗?
解决方法
目前无法实现。
见:https://github.com/microsoft/TypeScript/issues/15480
Exclude<number,200 | 400>
不起作用,因为打字稿只跟踪是什么,而从不跟踪它不是。为了从无限系列中排除某些值,打字稿必须为每个可能的值生成一个联合,除了您希望排除的那些值。这将是无限长的并集(因为无限减 2 等于无限)
也就是说,http 状态码列表实际上是有限的。所以最好的方法可能是创建所有可能值的联合并使用它:
type HTTPStatusCode = 100 | 101 | 102 | 103 | 200 | 201 | 202 | ...
现在应该可以正常工作了:
Exclude<HTTPStatusCode,200 | 400>