Skip to content

Rest tuple parameter with intersection/union places error on wrong argument #28505

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

Open
jcalz opened this issue Nov 13, 2018 · 2 comments
Open
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging
Milestone

Comments

@jcalz
Copy link
Contributor

jcalz commented Nov 13, 2018

TypeScript Version: 3.2.0-dev.20181113

Search Terms:
rest tuple, intersection, union, error, argument, misplaced

Code

declare function boop<T extends any[]>(...args: T & [number, string]): void;
boop(123, 456); // error
//   ~~~ <-- error on first argument
// Argument of type '[123, 456]' is not assignable to parameter 
//   of type '[number, number] & [number, string]'.
// Type '[123, 456]' is not assignable to type '[number, string]'.
// Type '456' is not assignable to type 'string'.

Expected behavior:
I expect an error on the second argument (456), since that's the argument that causes the failure, as made evident by the error message. (Or possibly I expect an error on both arguments, if it's interpreted as a failure to match the rest parameter.)

Actual behavior:
The actual error is on the first argument (123). This is a minor problem, and it only seems to show up in the intersection-with-type-parameter situation above (concrete types like ...args: [number, string] & [number, string] work fine), but I figured I'd report it in case it has an easy fix.

(Note to self or interested non-selves: ran into this in a Stack Overflow answer)

Playground Link:
🔗

Related Issues:
Haven't found anything. Anyone find anything?

@jack-williams
Copy link
Collaborator

Think the same happens with unions too:

declare function boop<T extends [number, string]>(...args: T | [number, boolean]): void;
boop(123, 456); // error
//   ~~~ <-- error on first argument
// Argument of type '[123, 456]' is not assignable to parameter 
//   of type '[number, string] & [number, boolean]'.
// Type '[123, 456]' is not assignable to type '[number, string]'.
// Type '456' is not assignable to type 'string'.

@jcalz
Copy link
Contributor Author

jcalz commented Jan 16, 2021

I see generics aren't even needed to reproduce this:

declare function f(...args: [x: string, y: number] | []): void;
f("a", "b"); // error
//~~~ <--- error here due to a problem with the *second* argument 😕

Playground link

@jcalz jcalz changed the title Inferred tuple type rest parameter with intersection seems to place error on wrong argument Tuple type rest parameter with intersection/union places error on wrong argument Jan 16, 2021
@jcalz jcalz changed the title Tuple type rest parameter with intersection/union places error on wrong argument Rest tuple parameter with intersection/union places error on wrong argument Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging
Projects
None yet
Development

No branches or pull requests

5 participants