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 inference doesn't work in recursive reference situation. #34910

Closed
zheeeng opened this issue Nov 5, 2019 · 1 comment
Closed

Type inference doesn't work in recursive reference situation. #34910

zheeeng opened this issue Nov 5, 2019 · 1 comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@zheeeng
Copy link

zheeeng commented Nov 5, 2019

TypeScript Version: 3.7-beta

Search Terms:

Code

type RefSelf<Input extends Record<string, (...args: any[]) => unknown>> = {
  [k in keyof Input]: () => ReturnType<Input[k]>
}

declare function calculate <
  Input extends Record<string, (self: RefSelf<Input>) => unknown>,
>(input: Input): RefSelf<Input>

const { fourtyTwo, fourtyTwo2 } = calculate({
  fourtyTwo: () => 42,
  // (parameter) self: RefSelf<{fourtyTwo: unknown; fourtyTwo2: unknown;}>
  // (property) fourtyTwo: () => any
  fourtyTwo2: (self) => self.fourtyTwo(),
})

Expected behavior:

  1. output of the calculate be inferred as { fourtyTwo: () => number, fourtyTwo2: () => number }

  2. self be inferred as RefSelf<{ fourtyTwo: unknown; fourtyTwo2: unknown; }>

Actual behavior:

  1. output of the calculate be inferred as { fourtyTwo: () => number, fourtyTwo2: () => any }

  2. self be inferred as RefSelf<{ fourtyTwo: () => number; fourtyTwo2: () => number; }>

Playground Link: http://www.typescriptlang.org/play/#code/C4TwDgpgBAShBmBlCAbeAeAkgOzAV2CggA9gJsATAZ1ggGMB7AJwvSuCYEtsBzAGigAKAHSiAhkx5UAXFDHYQAbQC6ASigBeAHxQ82ANbYGAd2xadGqAG8AUFCiL9UblH0QQDeFBz5gy2YLq2rTAeEzYACrgEFi4BI7KWjYAvjY2FPQoEtDwenTAnAzYUHRiKHR4WWRQ6HbecYQkZJQ0cIwsbBzc-EJUqPCycEj9sb5aQTp6hiZmfDZagty+sj4EqoMIyGijBEk2jNjs1lDwDGGgEcYMAqfnIJcMAExQyZolZRVVEIK29rdMFyuAQmUAALI85vYAPRQoRgCRiAC2EDITHUfTQG2G2ys-0BDFkUyMpgA3CczgD7ldHoSDMTsCTkklobDBGAmAxIJT1HiqQShCD5CA6ryHjTev0QRj4MJRVdAnNkqogA

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Nov 8, 2019
@RyanCavanaugh
Copy link
Member

We'd need multiple rounds of inference to be able to handle this situation correctly. Ref. #30134

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

2 participants