Open
Description
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.