首页 > 解决方案 > 我可以在 TypeScript 的非类装饰器中获取 typeof 对象吗?

问题描述

假设我有这样的代码。我想在道具装饰器中获得相同的 typeof 对象,但我不知道如何。

@classdecorator
class A {
    @propdecorator
    foo:string
}

const registry : Map<typeof A, Object> = new Map<typeof A, Object>()

function classdecorator<T extends typeof A> (target:T) {
  registry.set(target, {})
}

function propdecorator<T extends A> (target:T, key:string) {
  /* error:
    Argument of type 'T' is not assignable to parameter of type 'typeof A'.
  Property 'prototype' is missing in type 'A' but required in type 'typeof A'.ts(2345)
  */
  registry.set(target, {})   // How can I get the same target as the one in class decorator?
}

标签: typescript

解决方案


推荐阅读