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

Variadic tuple constraint incoherent ? #39713

Open
lifaon74 opened this issue Jul 23, 2020 · 1 comment
Open

Variadic tuple constraint incoherent ? #39713

lifaon74 opened this issue Jul 23, 2020 · 1 comment
Assignees
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@lifaon74
Copy link

TypeScript Version: 4.0.0-dev.20200722

Search Terms:
Variadic tuple
Variadic tuple constraint

Code

type A<GArgs extends [number, string, ...any[]]> = true;
type B<GArgs extends [string, ...any[]]> = A<[number, ...GArgs]>; // ERROR
type C = A<[number, ...[string, ...any[]]]>; // OK

Actual / Expected behavior:

On the type B we get an error:

TS2344: Type '[number, ...GArgs]' does not satisfy the constraint '[number, string, ...any[]]'.   Element at index 1 is variadic in one type but not in the other

Sounds incorrect because on B, GArgs is properly shaped to be just like in C

Playground Link

Related Issues:

The variadic issues are a big jungle, and I was not able to find any related issue (but probably some exists?)

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jul 23, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.0.1 milestone Jul 23, 2020
@ahejlsberg
Copy link
Member

Our relationship reasoning for variadic tuples is currently quite conservative, and we only match variadic elements against other variadic elements. In the example above, it is indeed the case that any instantiation of [number, ...GArgs] would satisfy the constraint [number, string, ...any[]], as is evident by substituting the constraint [string, ...any[]] in place of GArgs. However, it isn't generally the case that a variadic element's constraint can be substituted in its place. For example:

type A1<T extends [number, string?, boolean?]> = true;
type B1<U extends [string?]> = A1<[number, ...U, boolean?]>;

Here, the instantiation B1<[]> corresponds to A1<[number, boolean?]> which doesn't satisfy the constraint.

Certainly, in cases where a variadic element occurs in the last position of a tuple, and cases where a variadic element has a fixed length tuple constraint, we could do additional reasoning. So, I'll label this issue a suggestion to that effect.

@ahejlsberg ahejlsberg added Suggestion An idea for TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Jul 23, 2020
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 4.0.1 milestone Jul 23, 2020
@RyanCavanaugh RyanCavanaugh added the Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature label Jul 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants