在以下代码段中,由于双方差,y
的类型被推断为Bar<{}>
:
type Foo<A> = { type: "foo", x: A }
type Bar<A> = { type: "bar", y: A }
type FooBar<A> = Foo<A> | Bar<A>
type Opposite<A, FB extends FooBar<A>> = FB["type"] extends "foo" ? Bar<A> : Foo<A>
declare function process<A, FB extends FooBar<A>>(foo: FB): Opposite<A, FB>
const x: Foo<number> = null as any
const y = process(x) // Bar<{}>
我可以保留通用类型A
以便process(x)
返回Bar<number>
而不明确指定吗?
答案 0 :(得分:3)
基于A
的类型约束来推断FB
并不是打字稿真正可以做的事情。由于参数没有提供推断A
的站点,因此打字稿将推断A
的最大可能类型,即{}
您可以使用条件类型从类型参数A
中提取FB
:
type Foo<A> = { type: "foo", x: A }
type Bar<A> = { type: "bar", y: A }
type FooBar<A> = Foo<A> | Bar<A>
type Opposite<A, FB extends FooBar<A>> = FB["type"] extends "foo" ? Bar<A> : Foo<A>
type FooBarParam<T> = T extends FooBar<infer A> ? A : never;
declare function process<FB extends FooBar<any>>(foo: FB): Opposite<FooBarParam<FB>, FB>
const x: Foo<number> = null as any
const y = process(x) // Bar<number>