Repository: TypeScript
Issue: Contextual inference of functions in tuple types failures
Labels: Needs Investigation

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

<!--
  What search terms did you use when trying to find an existing bug report?
  List them here so people in the future can find this one more easily.
-->
contextual inference
tuple inference

### 🕗 Version & Regression Information

<!-- 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:
-->
This is the behavior in every version I tried, and I reviewed the FAQ for entries about contextual inference

### ⏯ 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?jsx=0&ts=4.6.0-dev.20220116#code/MYGwhgzhAEDKAuZ4FMA8AVAfNA3gWAChpoB6E6AJWXgFcAnAOxjGgDMaHh4BLAewejwAFkkFgA1shjwANNDAAHBSG5To3eIN7yQIaArq8AbtwAmyU2yYzCxMvIaWFNCELXDk0OlJoh40oUMaAHMhQTd9QwArZC5eOlt9GgAjFWBoYOoABWjYnn4AMTA4ugBPVApoZAAPFEcYbzBTfhBS6A5xBl4AdwYAbQBdOSzMAApE4gA6abA6YIgALmg+ieJoacnWJiWcZe51AUlS3lZKAaXR+CX0AEpoAF5sCj7uAegAXxsiNeIDXhiShcNmAlhQ7o9oFlVgNEjcLldoLcHtgsrhVvZuMEunRuAxguoALbKZAE5AMRD5BgbVbeWiMdqOZCsXEWeTMBilADciXehF5BEI8FKCk8BV42nuuDY4qWDBoBOSyDoH25BGA-AgmjAYt4CCQnklDGQ3TgFLQOrGN1VhHVTE0KE1AHkjQ95Dq9ShJpl4Dl-nk+AwiiVSuNvqNdqxxR9wdhpbwvsRRjHoAAmBOkciAXg3AOI7B3VdG8XFaB1YSu8lng2kVyw6XV6cjlCqVMLDwLmEGT4a8Pj8S1m82jhCthEI9kzgGq9thgbggejIZ2ebgwPq1noMBvyxV0N64zXIJrQE5VaoirisvqNrcbpvbm0azS7bwQXwI1jT2feBcfV0O+ALruRrwSwAMzRtaBBjrmuL5oW8DFripZ0DAWzSFWnijJcSw6smq69Dcgx3naghSPA6DdBKbrih6yBetkuRcAGQaVmUoaJsmACM6ZJsiqbpqgACCx51KYDT7s0DDFrh-QDGMbbzEs-Gdo+PYIv2MDvDcQ7gWOk5vjOc5kdoS4Mp0a6DAce4HkeNSniglgXpuSrXluLa2pqUpPi+SyeX4hnfpKv6GQBMrQKBGmqkAA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
class State<T> {
  // Returns a function that takes t, applies it to all provided fns,
  // and pushes the results through the projector
  public getProjectionFactory<R extends readonly unknown[], P>(
    ...args: [
      ...fns: { [i in keyof R]: (t: T) => R[i] },
      projector: (...a: R) => P
    ]
  ): (t: T) => P {
    // ignoring implementation...
    return undefined as any;
  }
}

type Foo = { foo: number };
const aFooState = new State<Foo>();

const testOne = aFooState.getProjectionFactory(
  ({ foo }) =>  foo,
  () => 2,
  // 👇 incorrectly inferred to be [unknown, number]
  (...args) => ({ result: args })
);

// 🚫 failureOne is [unknown, number] instead of expected [number, number]
const { result: failureOne } = testOne({ foo: 3 });

// 👇 incorrectly infers fns to be ((t: Foo) => unknown)[]
const testTwo = aFooState.getProjectionFactory(
  () => 1,
  () => 2,
  <A extends readonly unknown[]>(...args: A) => ({ result: args })
);

// 🚫 failureTwo is unknown[] instead of expected [number, number]
const { result: resultTwo } = testTwo({ foo: 3 });
```

### 🙁 Actual behavior

The `projector` and `fns` in the tuple type examples are incorrectly inferred.

### 🙂 Expected behavior

`projector` should be inferred as `(...args: [number, number]) => { result: [number, number] }` in the first test example.

`fns` should be inferred as `[(t: Foo) => number, (t: Foo) => number]` in the second test example.