TypeScript对类型的反思?

发布于 2025-02-08 21:17:15 字数 657 浏览 3 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷了相思 2025-02-15 21:17:15

不,本地打字稿不包含此类功能。但是您可以使用 tst-reflect

import { getType, Property, Type } from 'tst-reflect';

export enum eNum {
  FOO = 0,
  BAR = 1,
}

export type foo = {
  bar: string;
  baz?: number;
  qux?: eNum;
};

const fooType: Type = getType<foo>();

console.log({
  name: fooType.name,
  members: fooType.getProperties().map(mapProperty),
});

function mapProperty(prop: Property) {
  return {
    key: prop.name,
    definition: prop.type.name,
    optional: prop.optional,
  };
}

输出:

{
  name: 'foo',
  members: [
    { key: 'bar', definition: 'String', optional: false },
    { key: 'baz', definition: 'Number', optional: true },
    { key: 'qux', definition: 'eNum', optional: true }
  ]
}

在此处工作示例:

No, native TypeScript does not contain such feature. But You can use tst-reflect for example.

import { getType, Property, Type } from 'tst-reflect';

export enum eNum {
  FOO = 0,
  BAR = 1,
}

export type foo = {
  bar: string;
  baz?: number;
  qux?: eNum;
};

const fooType: Type = getType<foo>();

console.log({
  name: fooType.name,
  members: fooType.getProperties().map(mapProperty),
});

function mapProperty(prop: Property) {
  return {
    key: prop.name,
    definition: prop.type.name,
    optional: prop.optional,
  };
}

Output:

{
  name: 'foo',
  members: [
    { key: 'bar', definition: 'String', optional: false },
    { key: 'baz', definition: 'Number', optional: true },
    { key: 'qux', definition: 'eNum', optional: true }
  ]
}

Working example here: StackBlitz.

安静 2025-02-15 21:17:15

类型在运行时不存在,因此这是不可能的。

Types do not exist at runtime, so this is not possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文