Microsoft/TypeScript

Problem with function types, <T>() => T extends X ? () : ()

Open

#56,721 创建于 2023年12月9日

在 GitHub 查看
 (10 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
Cursed?Domain: check: Variance RelationshipsHelp WantedPossible Improvement

仓库指标

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

描述

🔎 Search Terms

"function", "generic function"

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAYgdgHgBoD4oF4oICooBQCUGa2UEAHsBHACYDOUSUA-FAIxQBcUATAFChIUAKIBHAK4BDADZ02yADRQAmmnR8osRKjKVq9DVoSqWUYACdx0bgDMZdCHwHhoAdXMB7OAHMAShDpxaWAMEQl7eQBvcwhJGi9pEChJbgByAEFUgF8lSJSoDOyUJydBaHh5HUwcfCJ0El0qWgYmVg5ufjKtHmQ1LFxCYihSCib6RlN23mchMSlZHqQlE3VNCt7G-TpDeB6TVgsrLig7WUcZ6ABhD3MYgGNgf0Dg0Ln7HujY+LhE5LTMnJQPL-IpAA

💻 Code

type Fn<X> = <T>() => T extends X ? 1 : 2
type Equals<X, Y> =
  Fn<X> extends
  Fn<Y> ? true : false

type A = Equals<{readonly a: 'A'}, {a: 'A'}>

🙁 Actual behavior

type A should shows true.

🙂 Expected behavior

type A should be false.

It works well when the code is like this.

type Fn1<X> = <T>() => T extends X ? 1 : 2
type Fn2<X> = <T>() => T extends X ? 1 : 2
type Equals<X, Y> =
  Fn1<X> extends
  Fn2<Y> ? true : false

type A = Equals<{readonly a: 'A'}, {a: 'A'}>

Additional information about the issue

No response

贡献者指南