【问题标题】:Question about typing decorators in TypeScript关于在 TypeScript 中键入装饰器的问题
【发布时间】:2021-03-29 02:34:36
【问题描述】:

是否可以在 TypeScript 中实现装饰器 @important 和实用程序类型 PickImportantProps,使类型 B 在以下示例中变为 { b: string }? 非常感谢提前:)


// decorator
function important(...): ... {
  ...
}

type PickImportantProps<T> = ...

class A {
  a = 'a'

  @important()
  b = 'b'
}

type B = PickImportantProps<A> // <= should result in { b: string }

【问题讨论】:

  • 这不太可能,但您可以查看此问题的答案以获得冗长的解决方法:stackoverflow.com/questions/58039676/…
  • 感谢 hlfrmn 的回答以及指向 Stack Overflow 上类似问题的链接。那里的答案和链接的 Github issue 非常有用。顺便说一句:有 50 学分奖励提供有用的答案。如果您有兴趣,请打开此问题的实际答案,因为我不能只为评论支付赏金。

标签: typescript typescript-decorator


【解决方案1】:

目前无法在编译时获取此信息。

this question 的答案显示了一种可能的解决方法,它基本上放弃了装饰器语法 (@decoratorName)。

【讨论】:

    猜你喜欢
    • 2019-05-11
    • 1970-01-01
    • 2018-09-02
    • 2019-02-23
    • 2018-06-08
    • 2017-11-15
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    相关资源
    最近更新 更多