文件AppActions.ts
export enum Actions{
START = 0,
RECOVER,
FIRST_RUN,
ALERT_NETWORK,
LOADING_DATA,
RECEIVED_DATA,
GO_OFFLINE,
GO_ONLINE
}
Run Code Online (Sandbox Code Playgroud)
文件PlayerActions.ts
import {Actions} from "./AppActions.ts"
enum Actions{
HEAL_SINGLE,
HIT_SINGLE
}
Run Code Online (Sandbox Code Playgroud)
通常,关于本手册,它应该在编译时引发错误。但:
1- PlayerActions.ts似乎没有扩展现有的Actions枚举。(在WebStorm中import {Actions} from "./AppActions.ts"为灰色)
2-编译器不会抛出任何错误。
那么在多个文件中声明Enum的正确方法是什么?
小智 1
Typescript 中似乎不可能有部分枚举,因为转换器会为每个“导出”构建全局类型。
一种可能的解决方案可能是合并两个(多个)枚举,如下所示:
在第一个文件中,您有:
export enum Action_first_half{
START = 0,
RECOVER,
...
}
Run Code Online (Sandbox Code Playgroud)
在另一个文件中你有:
export enum Action_second_half {
HEAL_SINGLE,
HIT_SINGLE
}
Run Code Online (Sandbox Code Playgroud)
然后在另一个文件中,您可以:
const Actions = { ...Action_second_half , ...Action_first_half};
type Actions = typeof Actions ;
Run Code Online (Sandbox Code Playgroud)
现在,您可以将“Actions”视为常规枚举:
public const action: Actions = Actions.START;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
416 次 |
| 最近记录: |