在下面的代码片段中,由于双变性,
我可以保留泛型类型
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>
而不必显式指定吗?