【问题标题】:Infer Class/Instance type from constructor function从构造函数推断类/实例类型
【发布时间】:2020-04-10 19:40:21
【问题描述】:

如何从构造函数推断类类型或实例类型?

编辑:我只得到了原型,并且想要取回类类型。请参阅装饰器示例。

Class User {}

// The following makes me really sad
User.prototype/* Object */.constructor/* Function */

属性装饰器示例:

// My code
// I cannot get class/instance type from T...
const Prop = <T>({ constructor }: T) => {}

// My user's code
class User {
  @Prop
  photos
}

谢谢

【问题讨论】:

    标签: typescript


    【解决方案1】:

    试试InstanceType:

    type UserInstance = InstanceType<User>
    

    【讨论】:

    • 嘿!我不清楚这一点,User 类是用户的代码,而不是我自己的......无论如何感谢您的回答!
    猜你喜欢
    • 2021-02-26
    • 2019-09-23
    • 2020-10-17
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    相关资源
    最近更新 更多