Repository: TypeScript
Issue: Function cannot be contextually typed when its rest parameter type is tuple type with leading rest elements
Labels: Suggestion,Awaiting More Feedback

# 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

<!-- When did you start seeing this bug occur?

"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
  https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly

Note: The TypeScript Playground can be used to try older versions of TypeScript.

Please keep and fill in the line that best applies:
-->
- I was unable to test this on prior versions because _leading rest elements in tuple types were introduced in TS 4.2_

### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior

  The TypeScript Workbench can be used for more complex setups, try
  https://www.typescriptlang.org/dev/bug-workbench/

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.4.2#code/C4TwDgpgBAYgrgOwMZQLxQBQDocEMBOA5gM4BcUA2jlscPmVLfgJYKEUC6ANFAnALYBGcn34AjCPh6iATCIET8HAJRoAfFABuAe2YATAFAGA9MagB5ANIGk2hLSgAzREnLxkaTKtQaA3gF8TMwBRfHxtfFIgqAAVcGgAcgwCQmFGOlZCKAAfXgVJbw0dfQSoZmJebWAoXGJiZkIEXDEAG2hgbShQSCgE9yQErGjY+IrtRygwAlx+CGBJCoSUwVLcBD1epmJV-GhWW34p4GZWiCHTKEuRnoSqHC3yJkzOaQE00UVX-jk88UkOUrlSrVWr1RrNNpdTrdRIUZaPDJsHK-RQA85mK6XADK2jg+CQ0AAFrUoDIoBA2rMEMAMMRVGI4NVgCk5jUWi1tAB3MYIFogKCCIa2ezVZzINL9TzJIiCQpQAJGC6hcKRaJxG7Swg-J5I3IfSQ8FIAZgRLF1KIK6i0uj0gIqCCqNTqDSapyhXXivX6g2G1wgYwmU3wMzmC16KRkq3Wmzo2xquzKyG0h1wx1O6Mxft6dxosdNz24v3e+Skvx++qUduBTrBrshHQ9NzhRG1iKyepLhqIJvSZvbFsrGczsRZIPZXJ5fNJ5MpEGptPpjMYuPx0H4uH5xM0a4iZxsdgcYqQP0l6E1Mi7hCNcoCAG5FSEwhEohd1YlNQAWfPmiuXgCs379r+8aEAAbIByIVnKxS2mU9qOqCLoQu00Ken0Lg+hclxvgGkzTLM8z0OGRAflGGwJFsOx7EmKZpm0Q5XG+2bUA8vYFl8xZ-KWsjyFxAJwdWiHgm6DYwtmKRfmxP6diBAFSUBMkpOB8mQSWaK+piMSjlAuwAI5wMwuwVEaM4QFSNJ0lADLVMQK4ElA66brg25OBAnKSEKB6ii4PanpgEn-peoE3v494GEAA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
type Func = (...args: [...strs: string[], num1: number, num2: number]) => void

// OK
const func: Func = () => {}
// Error:
// Type '(arg1: string | number) => void' is not assignable to type 'Func'.
//  Types of parameters 'arg1' and 'strs' are incompatible.
//    Type '[...strs: string[], num1: number, num2: number]' is not assignable to type '[arg1: string | number]'.
//      Source has 2 element(s) but target allows only 1.
const func1: Func = (arg1) => {}

// Error:
// Type '(arg2: string | number, arg3: string | number) => void' is not assignable to type 'Func'.
//   Types of parameters 'arg2' and 'strs' are incompatible.
//     Type '[...strs: string[], num1: number, num2: number]' is not assignable to type '[arg2: string | number, arg3: string | number]'.
//       Target allows only 2 element(s) but source may have more.
const func2: Func = (arg2, arg3) => {};

// Error:
// Type '(arg4: string | number, arg5: string | number, arg6: string | number) => void' is not assignable to type 'Func'.
//   Types of parameters 'arg4' and 'strs' are incompatible.
//     Type '[...strs: string[], num1: number, num2: number]' is not assignable to type '[arg4: string | number, arg5: string | number, arg6: string | number]'.
//       Target requires 3 element(s) but source may have fewer.
const func3: Func = (arg4, arg5, arg6) => {};
```

### 🙁 Actual behavior

Functions that have one or more arguments (`func1`, `func2`, `func3`) cannot be assigned to variables of type `Func`.

### 🙂 Expected behavior

No compiler errors and the types of arguments are inferred as:

- `arg1`, `arg2`, `arg3`, `arg4`, `arg5`: `string | number`
- `arg6`: `string | number | undefined`