如何在 TypeScript 中编写枚举的别名

问题描述 投票:0回答:1

我正在重构一个大型代码库,其中包含以下定义:

export const enum Direction {
    NORTH,
    SOUTH,
}

还有很多这样的代码:

console.log(Direction.NORTH);

新的定义是这样的:

namespace abc {
    export const enum Direction {
        NORTH,
        SOUTH,
    }
}

有了它,我可以这样写:

console.log(abc.Direction.NORTH);

不过,我不想更改上面的代码来访问枚举值。

我试着写这个:

namespace abc {
    export const enum Direction {
        NORTH,
        SOUTH,
    }
}

console.log(abc.Direction.NORTH);

export type Direction = abc.Direction;

console.log(Direction.NORTH);

但是,在最后一行,我收到错误:

'Direction' only refers to a type, but is being used as a value here.
typescript enums namespaces
1个回答
0
投票

将 Direction 导出为类型和值:

namespace abc {
    export const enum Direction {
        NORTH,
        SOUTH,
    }
}

export import Direction = abc.Direction;
© www.soinside.com 2019 - 2024. All rights reserved.