TypeScript – 使用特定装饰器装饰的属性创建联合类型

huangapple go评论54阅读模式
英文:

TypeScript - Union type from properties decorated with a specific decorator

问题

class Image {
  @Attribute()
  public id: number;

  @Attribute()
  public src: string;

  public alt: string;
}

// ?? 应该得到 "id", "src"
type ImageAttributes = ExtractAttributes<typeof Image>
英文:

Is it possible to create a generic type, which accepts a class, some of whose properties are decorated with a specific decorator, and create a type out of them

class Image {
  @Attribute()
  public id: number;

  @Attribute()
  public src: string;

  public alt: string;
}

// ?? Should result in &quot;id&quot;, &quot;src&quot;
type ImageAttributes = ExtractAttributes&lt;typeof Image&gt; 

答案1

得分: 1

目前还没有指示字段已被装饰的类型指示。与 TypeScript 几乎一样古老的问题1仍然存在。

所以,截止到今天,没有。
1: https://github.com/microsoft/TypeScript/issues/4881

英文:

Currently there is no type indication that a field has been decorated. The open issues on that are almost as old as TS.

So, as of today, no.

huangapple
  • 本文由 发表于 2023年2月18日 17:46:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定