Skip to content
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

Mixing tuple & generics #1011

Closed
srenault opened this issue Oct 31, 2014 · 5 comments
Closed

Mixing tuple & generics #1011

srenault opened this issue Oct 31, 2014 · 5 comments
Assignees
Labels
Bug A bug in TypeScript Spec Issues related to the TypeScript language specification

Comments

@srenault
Copy link

Hi, don't understand why this code doesn't compile:

function f<T>(value: [string, T]): T {
  return value[1];
}
var h: boolean = f(["string", true]);
error TS2323: Type 'string | boolean' is not assignable to type 'boolean'.
Type 'string' is not assignable to type 'boolean'.

Someone can help me ?

@mhegazy mhegazy added the Bug A bug in TypeScript label Oct 31, 2014
@RyanCavanaugh RyanCavanaugh added the Spec Issues related to the TypeScript language specification label Oct 31, 2014
@RyanCavanaugh
Copy link
Member

Technically by design, but the design should probably change. Under discussion.

@JsonFreeman
Copy link
Contributor

We have special logic for inferring to a union type. But I think we need to say that if we are inferring from a union type, certain constituents of the target can "use up" certain constituents of the source. Then the remainder of the source constituents can be inferred to the target's plain type parameter constituent if it exists.

@JsonFreeman
Copy link
Contributor

Sorry it was probably unclear why I was talking about union types. It's because the tuple type is an Array of the union elements. So the tuple type in the parameter is an Array<string | T> and the argument is a tuple that extends Array<string | boolean>.

@srenault
Copy link
Author

srenault commented Nov 1, 2014

Ok thanks for the explanation !

@JsonFreeman
Copy link
Contributor

Discussed with @ahejlsberg and we decided that the type argument inference candidates should be stratified. In this case, the thing we are inferring to has the following shape:

... extends Array<string | T> {
   0: string;
   1: T;
}

and the argument has the following shape:

... extends Array<string | boolean> {
   0: string;
   1: boolean;
}

The bug is that we infer two candidates for T, namely boolean (from the 1 property) and string | boolean (from the Array portion of the tuple). The former is a good inference. The latter is not such a good inference, and it comes from trying to infer from string | boolean to string | T. Because it doesn't make a whole lot of sense to infer to a bare type parameter that is part of a union type, we'd like this inference to be secondary to the good inferences. So we shall have primary inferences and secondary inferences. The secondary inferences can be used as a fallback in case the primary inferences are absent or conflicting. But we cannot eliminate these secondary inferences because we need them in order for Promises to be inferred correctly.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Spec Issues related to the TypeScript language specification
Projects
None yet
Development

No branches or pull requests

6 participants