如何在打字稿中正确地别名“枚举”类型(及其对象)?

发布于 2025-02-11 04:58:57 字数 1115 浏览 2 评论 0原文

我们将一些代码从闭合类型系统转换为打字稿。以前,我们有一些@enum s和@typedef s,这些 s被导出为类的静态成员:

export class C {}

/** @enum {number} */
C.E = {v0: 0, v2: 1, v2: 2};

/** @typedef { ... omitted ... } */
C.T;

看来Typescript不支持声明类型是静态成员类(通过static类声明中的关键字或通过语法(例如enum ce {…;而不是合并:

export class C {}

export namespace C {
  export enum E = {v0, v1, v2};
  export type T: /* omitted */;
}

这是

我们希望将这些模块作为类的静态属性过渡到自己的静态属性,从而将这些类型的属性自行分开,但在两个地方导出了它们。这是?

对于Typedef,简单导出t = ct;似乎就足够了,但是对于枚举,似乎可以写入:

export type E = C.E;

导出类型,而不是对象(e e)将在编译的JavaScript中不确定),或

export const E = C.E;

导出对象,而不是导出类型(E将定义,但试图将其用作类型产生的类型”'e'是指一个值,但在此处被用作类型“错误)。

另外,如果我们想为去除静态属性做准备,是否有一些好方法将声明从合并的名称空间中移出,同时仍将其重新效仿?即,类似:

export class C {}
export enum E = {v0, v1, v2};
export type T: /* omitted */;

export namespace C {
  export type T = /* ??? */;  // T = T does not work, for obvious reasons.
  export /* ??? */ C = /* ??? */;
}

We're converting some code from the Closure type system to TypeScript. Previously we had some @enums and @typedefs that were exported as static members of a class:

export class C {}

/** @enum {number} */
C.E = {v0: 0, v2: 1, v2: 2};

/** @typedef { ... omitted ... } */
C.T;

It seems that TypeScript doesn't support declaring types to be static members of a class (either via the static keyword in the class declaration or via syntax like enum C.E {…; instead, it appears that the preferred way to maintain backwards compatibility is via declaration merging:

export class C {}

export namespace C {
  export enum E = {v0, v1, v2};
  export type T: /* omitted */;
}

and this works as expected.

We'd like to transition this module from exporting these types as static properties of the class to separate named exports in their own right—but export them in both places transitionally. How can one do this?

For the typedef, a simple export type T = C.T; seems to suffice, but for the enum it appears that one can write:

export type E = C.E;

to export the type but not the object (E will be undefined in the compiled JavaScript), or

export const E = C.E;

to export the object but not the type (E will be defined, but trying to use it as a type produces "'E' refers to a value, but is being used as a type here" errors).

Also, if we want to prepare for the removal of the static properties, is there some good way to move the declarations out of the merged namespace, while still reexporting them there? I.e., something like:

export class C {}
export enum E = {v0, v1, v2};
export type T: /* omitted */;

export namespace C {
  export type T = /* ??? */;  // T = T does not work, for obvious reasons.
  export /* ??? */ C = /* ??? */;
}

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

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

发布评论

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

评论(2

旧梦荧光笔 2025-02-18 04:58:57

似乎有效但似乎是优势的一种解决方案是使用声明合并为重新出口:

export type E = C.E;
export const E = C.E;

One solution which appears to work, but which seems sub-optimal, is to use declaration merging for the re-exports:

export type E = C.E;
export const E = C.E;
诗化ㄋ丶相逢 2025-02-18 04:58:57

此外,还可以从特殊别名导入的名称空间中导入枚举;

import { C } from './c';
import E = C.E;

参考: testempript:documentript:documentation:documentation -documentation -namespaces

Also enums can be import from namespace with special aliased import;

import { C } from './c';
import E = C.E;

Reference: TypeScript: Documentation - Namespaces

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