我有这个TypeScript文件:
export type TSumanToString = () => string;
export interface ISumanEvent {
explanation: string,
toString: TSumanToString
}
export interface ISumanEvents{
[key: string]: ISumanEvent
}
export const events: ISumanEvents = Object.freeze({
// runner events
TEST_FILE_CHILD_PROCESS_EXITED: {
explanation: 'runner is started, fires before any test child processes are started.',
toString: makeToString('TEST_FILE_CHILD_PROCESS_EXITED')
},
RUNNER_EXIT_CODE: {
explanation: 'runner is started, fires before any test child processes are started.',
toString: makeToString('RUNNER_EXIT_CODE')
},
RUNNER_EXIT_SIGNAL: {
explanation: 'runner is started, fires before any test child processes are started.',
toString: makeToString('RUNNER_EXIT_SIGNAL')
},
RUNNER_HIT_DIRECTORY_BUT_NOT_RECURSIVE: {
explanation: 'runner is started, fires before any test child processes are started.',
toString: makeToString('RUNNER_HIT_DIRECTORY_BUT_NOT_RECURSIVE')
},
RUNNER_EXIT_CODE_IS_ZERO: {
explanation: 'runner is started, fires before any test child processes are started.',
toString: makeToString('RUNNER_EXIT_CODE_IS_ZERO')
},
RUNNER_TEST_PATHS_CONFIRMATION: {
explanation: 'runner is started, fires before any test child processes are started.',
toString: makeToString('RUNNER_TEST_PATHS_CONFIRMATION')
},
// there are a lot more entries
})
这会生成一个.d.ts文件,如下所示:
export declare type TSumanToString = () => string;
export interface ISumanEvent {
explanation: string;
toString: TSumanToString;
}
export interface ISumanEvents {
[key: string]: ISumanEvent;
}
export declare const events: ISumanEvents;
但我需要将每个属性包含在事件中作为常量或枚举。我可以使用一个界面来做到这一点,但这需要我复制一切,这似乎是浪费。
换句话说,我不希望这样做,因为这只是复制了我的const中的内容,这是一种浪费,很容易变得不合适-sync:
export interface ISumanEvents {
TEST_FILE_CHILD_PROCESS_EXITED: ISumanEvent,
RUNNER_EXIT_CODE: ISumanEvent,
RUNNER_EXIT_SIGNAL: ISumanEvent,
RUNNER_HIT_DIRECTORY_BUT_NOT_RECURSIVE: ISumanEvent;
RUNNER_EXIT_CODE_IS_ZERO: ISumanEvent,
RUNNER_TEST_PATHS_CONFIRMATION: ISumanEvent
}
有没有办法创建枚举,这样我就不必创建一个只复制实际对象中所有内容的界面?
问题是我不知道如何在枚举上定义方法。
答案 0 :(得分:1)
您是否有理由不能使用内置的Typescript枚举?