2017-07-05 4 views
1

Je possède ce fichier tapuscrit:Utilisation ENUM avec tapuscrit, avec des méthodes de chaque énumération

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 

}) 

ce produit un fichier .d.ts comme ceci:

export declare type TSumanToString =() => string; 
export interface ISumanEvent { 
    explanation: string; 
    toString: TSumanToString; 
} 
export interface ISumanEvents { 
    [key: string]: ISumanEvent; 
} 
export declare const events: ISumanEvents; 

mais je dois inclure chaque propriété sur les événements comme une constante ou une énumération. Je pourrais utiliser une interface pour faire cela, mais cela exigerait que je duplique tout, ce qui me semble être une perte.

En d'autres termes, je ne veux pas à cela, parce que cela fait double emploi avec simplement ce qui est dans mon const, ce qui est une perte, et pourrait facilement devenir hors de synchronisation:

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 

} 

existe-t-il un moyen de créer une énumération afin que je n'ai pas besoin de créer une interface qui duplique tout dans l'objet réel?

Le problème est que je ne sais pas comment définir les méthodes sur une énumération.

Répondre