这个问题是关于使用枚举而不是字符串文字的最佳实践。
我有以下接口,并且我将“type”属性的字符串文字重构为枚举以获取更多 DRY 代码。
export interface AppController {
restart: (
type: "end" | "exit,
id: string,
continueAt?: number
) => void;
}
我的枚举是:
export enum AppRestartTypes {
END = "end",
EXIT = "exit",
}
我现在重构的界面是:
export interface AppController {
restart: (
type: AppRestartTypes.END | AppRestartTypes.EXIT,
id: string,
continueAt?: number
) => void;
}
请注意 OR 仍然存在。只使用枚举是标准的吗?见下文:
export interface AppController {
restart: (
type: AppRestartTypes,
id: string,
continueAt?: number
) => void;
}
在我看来这很好,但是如果枚举中有三个属性(不仅仅是两个),但“类型”只能是其中两个呢?
谢谢