Closed
Description
Bug Report
π Search Terms
- Return types
- Type inference
π Version & Regression Information
- This is a bug
- This happens on all of the latest versions usable in the playground (4.1.3, 4.2.0-beta, even nightly)
β― Playground Link
Playground link with relevant code
π» Code
declare function combined<T>(obj: {
f1: (fixedParam: string) => T;
f2: (inferredParam: T) => unknown;
}): unknown;
// Case 1
combined({
f1: (fixedParam) => 1,
f2: (inferredParam) => {}, // unknown
});
// Case 2
combined({
f1: (fixedParam: any) => 1,
f2: (inferredParam) => {}, // number
});
// Case 3
combined({
f1: () => 1,
f2: (inferredParam) => {}, // number
});
declare function separated<T>(
obj1: { f1: (fixedParam: unknown) => T },
obj2: { f2: (inferredParam: T) => unknown },
): unknown;
// Case 4
separated(
{ f1: (fixedParam) => 1 },
{ f2: (inferredParam) => {} }, // number
);
π Actual behavior
There is a function with a type argument T
. It requires an object as an argument that has two properties:
- A function that returns
T
- A function that requires
T
as an argument.
If the first function requires any argument that must be inferred from the signature, TypeScript will fail to infer T
(Case 1).
This behavior does not occur when:
fixedParam
is explicitly typed (Case 2) or not used (Case 3).
or- Two inner functions are in separate arguments of outer functions (Case 4).
π Expected behavior
All inferredParam
should be inferred as number
. This feature is useful when struggling with handling complex data structure in React.