Skip to content

Type arguments used in return types of argument functions are not correctly inferred when wrapped in objectsΒ #42379

Closed
@chelproc

Description

@chelproc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions