-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Inconsistent inference from a rest parameter #30824
Comments
This is an intentional difference. Consider this code: type Action<T extends string = string> = { type: T };
declare const a: Action<"A">, b: Action<"B">, c: Action<"C">;
declare function f<T extends Action>(...args: T[]): T[];
declare function g<T extends Action>(args: T[]): T[];
declare function h<T>(a: T, b: T, c: T): T[];
g([a, b, c]);
f(a, b, c); // ?
h(a, b, c);
The question is whether it's more correct/consistent for |
Thanks. BTW, the workaround appears to be this. type Action<T extends string = string> = { type: T };
declare function f<A extends Action, R extends A[]>(...args: R): R;
const A = "A";
const B = "B";
const C = "C";
declare const a: Action<typeof A>;
declare const b: Action<typeof B>;
declare const c: Action<typeof C>;
const r1 = f(a, b, c); |
The above workaround can be simplified; the // ...
declare function f<R extends Action[]>(...args: R): R;
// ... |
TypeScript Version: 3.5.0-dev.20190407 and 3.4 in TS playground
Search Terms: infer rest tuple
Code
Expected behavior:
In all of the calls to
f
andg
in the example above, I would have expected no errors and the result to be inferred correctly.Actual behavior:
Arguments passed to the function that accepts a rest parameter -
f
- work only if said arguments are literals or are spread from an array/tuple. If separate, declared variables are passed, an error is effected, stating that the arguments are expected to be of the same type.Both variables and literals behave as expected with
g
- a function that accepts an array parameter, rather than a rest parameter.Playground Link:
Here is the playground link
Related Issues: None found
The text was updated successfully, but these errors were encountered: