Typescript 除 X 以外的任何数字
Typescript any number except X
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 状态代码列表实际上是有限的。所以最好的方法可能是创建所有 possible 值的并集并使用它:
type HTTPStatusCode = 100 | 101 | 102 | 103 | 200 | 201 | 202 | ...
现在应该可以正常工作了:
Exclude<HTTPStatusCode, 200 | 400>
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 状态代码列表实际上是有限的。所以最好的方法可能是创建所有 possible 值的并集并使用它:
type HTTPStatusCode = 100 | 101 | 102 | 103 | 200 | 201 | 202 | ...
现在应该可以正常工作了:
Exclude<HTTPStatusCode, 200 | 400>