Skip to content

Contextual inference of functions in tuple types failuresΒ #47226

Open
@david-shortman

Description

@david-shortman

Bug Report

πŸ”Ž Search Terms

contextual inference
tuple inference

πŸ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about contextual inference

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

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.

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions