英文:
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 "id", "src"
type ImageAttributes = ExtractAttributes<typeof Image>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论