typescript - 缩小打字稿中的generec类型
问题描述
typescript v4.4.4 为什么泛型类型在类型保护之后没有缩小?我如何解决它?
type Data = A | B | C
type G<T extends Data> = {
type: 'a' | 'b'
data: T
}
type A = {
foo: string
}
type B = {
bar: string
}
type C = {
foobar: string
}
const isA = (item: G<Data>): item is G<A> => item.type === 'a'
const throwOnA = (item: G<Data>): G<Exclude<Data, A>> => {
if (!isA(item)) return item // Item still G<Data> instead of G<B | C>
throw Error('is A')
}
解决方案
我认为这是因为 Typescript 无法缩小范围G<Data>
。它只知道item
是类型G<Data>
而不是类型G<A>
,但不能断定生成的泛型类型应该是G<B | C>
。我最好的猜测是它的类型解析器是一个限制。
作为一种解决方法,您可以为相反的检查创建一个函数:
const isNotA = (item: G<Data>): item is G<Exclude<Data, A>> => item.type !== 'a' // or return !isA(item)
const throwOnA = (item: G<Data>): G<Exclude<Data, A>> => {
if (isNotA(item)) return item // Item is now G<B | C>
throw Error('is A')
}
我知道这会带走一些魔力,但这是我能想到的最好的了。
推荐阅读
- asp.net-core - 调用操作时,会话被清除.Net Core
- python - 为什么这段代码会无限循环?
- python-3.x - 有什么办法可以让公会对象一个命令运行呢?
- javascript - axios post请求提交后抛出431错误
- mysql - Mysql 错误代码:1329。无数据 - 提取、选择或处理零行
- asp.net - Light Gallery 中的延迟加载
- amazon-web-services - 实时查看订阅状态
- c# - 如何在 ASP NET Core 3.1 中任何控制器的每个方法中检查/重定向?
- python - bs4 BeautifulSoup - 找不到看起来像自定义标签的东西来挽救我的生命
- django - 如何将函数名作为动态 url 的参数传递?