Microsoft/TypeScript

Case for inference failure in `T extends F<T>`

Open

#51,377 opened on Nov 1, 2022

View on GitHub
ย (5 comments)ย (0 reactions)ย (0 assignees)TypeScriptย (48,455 stars)ย (6,726 forks)batch import
Experience EnhancementHelp WantedSuggestion

Description

Bug Report

๐Ÿ”Ž Search Terms

circular type parameter constraint

Related issues: #40439 #30134

๐Ÿ•— Version & Regression Information

Tested with 4.8.4

โฏ Playground Link

Playground

๐Ÿ’ป Code

declare const f:
  <T extends F<T>>(t: T) => T

type F<T> =
  { a: unknown
  , b: (a: T extends { a: infer X } ? X : never) => unknown
  }

f({
  a: "hello",
  b: x => x.toUpperCase()
})
// doesn't compile because infers `x` as `unknown` instead of `string`

f({
  a: "hello",
  b: (x: string) => x.toUpperCase()
})
// compiles

Note that there is a workaround but it's only a workaround for this minimal case and not for the real world case from which this minimal case was derived.

๐Ÿ™ Actual behavior

x in the first f call gets inferred as unknown

๐Ÿ™‚ Expected behavior

x in the first f call should get inferred as string

Contributor guide

Case for inference failure in `T extends F<T>` ยท Microsoft/TypeScript#51377 | Good First Issue