Search code examples
javascripttypescriptstring-literals

TypeScript resuse string literals?


Is there a way to reuse string literals so I am not copy/pasting? something like this:

export class SendingStates {
    public static sendingStates: 'waiting' | 'sending' | 'sent' | 'error' | 'input-error'
}

interface IProps {
    sendingState: SendingStates.sendingStates
}

Basically, I want to have access to the 'sendingStates' type in multiple classes - thank you!


Solution

  • It's possible by defining a type:

    export type AVAILABLE_SENDING_STATES = 'waiting' | 'sending' | 'sent' | 'error' | 'input-error';
    

    Then use it where you want as a type:

    interface IProps {
        sendingState: AVAILABLE_SENDING_STATES
    }