TypeScript对类型的反思?
我有一个typescript
type
定义为:
export enum eNum {
FOO = 0,
BAR = 1
}
export type foo = {
bar: string,
baz?: number,
qux?: eNum
};
并且希望能够将反射数据达到以下内容:
{
name: "foo",
members: [
{
key: "bar",
optional: false,
definition: "string"
},
{
key: "baz",
optional: true,
definition: "number"
},
{
key: "bar",
optional: true,
definition: "eNum"
}
]
}
这是可能的吗?支持type
构造的反射程度?
I have a TypeScript
type
defined by:
export enum eNum {
FOO = 0,
BAR = 1
}
export type foo = {
bar: string,
baz?: number,
qux?: eNum
};
And would like to be able to attain reflection data into something like:
{
name: "foo",
members: [
{
key: "bar",
optional: false,
definition: "string"
},
{
key: "baz",
optional: true,
definition: "number"
},
{
key: "bar",
optional: true,
definition: "eNum"
}
]
}
Is this possible or does TypeScript
not support that degree of reflection on type
constructs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,本地打字稿不包含此类功能。但是您可以使用 tst-reflect 。
输出:
在此处工作示例:
No, native TypeScript does not contain such feature. But You can use tst-reflect for example.
Output:
Working example here: StackBlitz.
类型在运行时不存在,因此这是不可能的。
Types do not exist at runtime, so this is not possible.