Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
chelproc opened this issue Jan 17, 2021 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@chelproc
Copy link

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.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jan 19, 2021
@RyanCavanaugh
Copy link
Member

The inference algorithm doesn't support this sort of setup (and can't be "fixed" to do so). See #30134

@jcalz
Copy link
Contributor

jcalz commented Oct 9, 2022

Fixed by #48538, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants