Microsoft/TypeScript

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

Open

#51,377 创建于 2022年11月1日

在 GitHub 查看
 (5 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
Experience EnhancementHelp WantedSuggestion

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

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

贡献者指南