Closed
Description
TypeScript Version:
nightly (1.9.0-dev.20160528-1.0)
Code
A generic that is used twice in the argument list cannot always be resolved when a union type is used.
function bar<U>(x: string | U, y: string | U) {}
bar(4, "");
function foo<T>(xs: (T | string)[], ys: (T | string)[]) {}
foo([{ x: true }], [""]);
Expected behavior:
No errors, U
would be resolved as number
and T
as { x: boolean }
Actual behavior:
These errors:
The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
Type argument candidate 'number' is not a valid type argument because it is not a supertype of candidate 'string'.
The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
Type argument candidate '{ x: boolean; }' is not a valid type argument because it is not a supertype of candidate 'string'.
As a work around I can use two type arguments and take the union of them (foo2<T, V>(xs: (T | string)[], ys: (V | string)[]): (T | V)[]
), but that doesn't look pretty and I got some issues with an empty array (#8878).