如何在单个打字稿文件中声明多个枚举并在单个导出语句下返回(以免直接揭露单个枚举)

发布于 2025-02-09 08:04:41 字数 631 浏览 0 评论 0原文

我想在同一文件中有多个枚举,并在单个导出语句中导出它们。当i 导入在另一个文件中此文件时,我可以根据需要访问任何特定的枚举。

我当前拥有的内容:2 Secleate .ts动作和组枚举的文件,

first .ts file:

export enum Actions {
   Delete: "Delete",
   Update: "Update"
}

second .ts file:

export enum Groups {
   Lead: "Lead",
   Permanent: "Permanent"
}

预期:我想要一个.ts文件和单个导出语句。使用此导出,我可以访问我的两个枚举。 类似:enums.actions或enums.groups

组,只能通过枚举或枚举访问操作。其他一些文件应该无法直接访问单个枚举。

建议这样做的建议是什么?

I want to have multiple enums in the same file and export them under single export statement. and when I import this single file in another file, I can access any particular enum as required.

What I current have: 2 seperate .ts file for Actions and Groups enums,

First .ts file:

export enum Actions {
   Delete: "Delete",
   Update: "Update"
}

Second .ts file:

export enum Groups {
   Lead: "Lead",
   Permanent: "Permanent"
}

Expected: I want a single .ts file and a single export statement. Using this export, I can access my both the enums. something like: Enums.Actions or Enums.Groups.

Groups, Actions should only be accessed by Enums.Actions or Enums.Groups? some other file should not be able to access individual enums directly.

What is the suggested way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

貪欢 2025-02-16 08:04:41

您可以这样做:

export namespace Enum {
  export enum Actions {
    Delete = "Delete",
    Update = "Update"
  }

  export enum Groups {
    Lead = "Lead",
    Permanent = "Permanent"
  }
}

如何在文件中使用:

import {Enum} from "../Enum"

我们可以使用enum.actionsenum.groups访问。将枚举封装在命名空间中,允许操作和组不直接从外部访问。

you can do it like this:

export namespace Enum {
  export enum Actions {
    Delete = "Delete",
    Update = "Update"
  }

  export enum Groups {
    Lead = "Lead",
    Permanent = "Permanent"
  }
}

How to use in a file:

import {Enum} from "../Enum"

We can access using Enum.Actions and Enum.Groups. Encapsulating enums in a namespace, allows Actions and Groups to not directly get accessed from outside.

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