Skip to content

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

Closed
@zheeeng

Description

@zheeeng

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:

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