微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

打字稿除 X 外的任何数字

如何解决打字稿除 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 状态码列表实际上是有限的。所以最好的方法可能是创建所有可能值的联合并使用它:

type HTTPStatusCode = 100 | 101 | 102 | 103 | 200 | 201 | 202 | ...

现在应该可以正常工作了:

Exclude<HTTPStatusCode,200 | 400>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。