-
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
Function cannot be contextually typed when its rest parameter type is tuple type with leading rest elements #45972
Comments
Assuming we want to support such assignments, I think you don't want
If |
Thank you for pointing out 😃 you're right, I updated my issue comment. |
Although we can wrap all the arguments inside a tuple with rest syntax as a workaround, the type of type Func = (...args: [...strs: string[], num1: number, num2: number]) => void
const func: Func = () => {}
const func1: Func = (...[arg1]) => {}
const func2: Func = (...[arg2, arg3]) => {};
const func3: Func = (...[arg4, arg5, arg6]) => {}; Playground link with the above code Edit: The above code works because the arguments are being inferred as a whole. Adding types makes it break: type Func = (...args: [...strs: string[], num1: number, num2: number]) => void
// OK
const func: Func = () => {}
// Error:
// Type '(__0_0: string | number) => void' is not assignable to type 'Func'.
// Types of parameters '__0_0' and 'strs' are incompatible.
// Type '[...strs: string[], num1: number, num2: number]' is not assignable to type '[string | number]'.
// Source has 2 element(s) but target allows only 1.
const func1: Func = (...[arg1]: [string | number]) => {}
// Error:
// Type '(__0_0: string | number, __0_1: string | number) => void' is not assignable to type 'Func'.
// Types of parameters '__0_0' and 'strs' are incompatible.
// Type '[...strs: string[], num1: number, num2: number]' is not assignable to type '[string | number, string | number]'.
// Target allows only 2 element(s) but source may have more.
const func2: Func = (...[arg2, arg3]: [string | number, string | number]) => {};
// Error:
// Type '(__0_0: string | number, __0_1: string | number, __0_2: string | number | undefined) => void' is not assignable to type 'Func'.
// Types of parameters '__0_0' and 'strs' are incompatible.
// Type '[...strs: string[], num1: number, num2: number]' is not assignable to type '[string | number, string | number, string | number | undefined]'.
// Target requires 3 element(s) but source may have fewer.
const func3: Func = (...[arg4, arg5, arg6]: [string | number, string | number, string | number | undefined]) => {}; Playground link with the above code In any case, this issue should be a bug, not a suggestion. |
Bug Report
🔎 Search Terms
function rest arguments parameter tuple type leading middle rest elements
🕗 Version & Regression Information
4.2.3, 4.3.5, 4.4.2
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Functions that have one or more arguments (
func1
,func2
,func3
) cannot be assigned to variables of typeFunc
.🙂 Expected behavior
No compiler errors and the types of arguments are inferred as:
arg1
,arg2
,arg3
,arg4
,arg5
:string | number
arg6
:string | number | undefined
The text was updated successfully, but these errors were encountered: