编译器是否有可能接受这一点?
interface F<A> {
map<B>(f: (a: A) => B): F<B>
}
function foo<A, B>(f: F<A> | F<B>, g: (aOrB: A | B) => string): F<string> {
return f.map(g);
}
declare const f: Promise<string | number>;
// f is the wrong type.
foo<string, number>(f, x => x.toString);
哪个失败了:
Argument of type 'Promise<string | number>' is not assignable to parameter of type 'F<string> | F<number>'.
Property 'map' is missing in type 'Promise<string | number>' but required in type 'F<number>'.(2345)
在 F<A>
和 G<B>
的一般情况下,当然 F<A | B>
不是 F<A> | G<B>
。 G
是 F
那么 F<A | B>
不是 F<A> | F<B>
吗?
F<A> | F<B>
确实是一个 F<A | B>
(如 foo
所示,它正在扩大 f
以接受 g
)。据我所知,foo
的唯一实现是 f
被视为 F<A | B>
。如果是这种情况,那么 F<A> | F<B>
不应该等于 F<A | B>
吗?