【问题标题】:Type inference of generic types泛型类型的类型推断
【发布时间】:2019-04-18 18:44:37
【问题描述】:

我试图做这样的事情。关键是我有一些Interface,然后是一个Goo 类,它有一个扩展所述接口的泛型类型。

interface Interface { a: string; }
class Goo<T extends Interface> { props: T }
class Foo<T extends Goo<T["props"]>> {}

我想创建一个Foo 类,它采用扩展此泛型Goo 的泛型类型。问题是 Goo 需要一个通用参数,我尝试使用 T["props"] 来表示,但语法检查器显示该类型不满足接口的错误。

该类的用法如下所示:

class Impl implements Interface { a: string; }
type Boo = Foo<Goo<Impl>>;

【问题讨论】:

  • class Foo&lt;T extends Goo&lt;Interface&gt;&gt; {} 呢?
  • 是的,这确实有效,并且在更高级的情况下不会将类型缩小到界面。
  • 如果某些用例的建议不起作用,您能否编辑您的问题以包含一个最小示例?
  • @series0ne 建议的解决方案有效,但我不确定如何关闭此问题,因为它只是评论而不是回复。
  • 我将其添加为答案

标签: typescript generics


【解决方案1】:

你可以这样做

class Foo&lt;T extends Goo&lt;Interface&gt;&gt; {}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    相关资源
    最近更新 更多