Skip to content

Inference fails for type argument for two arguments with unions #8879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ivogabe opened this issue May 28, 2016 · 1 comment
Closed

Inference fails for type argument for two arguments with unions #8879

ivogabe opened this issue May 28, 2016 · 1 comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@ivogabe
Copy link
Contributor

ivogabe commented May 28, 2016

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).

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Aug 18, 2016
@RyanCavanaugh
Copy link
Member

Works as expected now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants