userMod2
userMod2

Reputation: 8960

Typescript - Interface of string values

I have a property which can basically contain 4 possible strings.

At the moment I'm using a simple | however I need to reuse those types else where, however how do I create an interface for just those 4 values:

selectedState?: "" | "IN_PROGRESS" | "SUCCESS" | "ERROR"

I was hoping to do something like:

interface SelectedStates: "" | "IN_PROGRESS" | "SUCCESS" | "ERROR"

and then

selectedState?: SelectedStates

Any ideas appreciated.

Upvotes: 1

Views: 37

Answers (1)

pzaenger
pzaenger

Reputation: 11973

You could use a type alias:

Type aliases create a new name for a type. Type aliases are sometimes similar to interfaces, but can name primitives, unions, tuples, and any other types that you’d otherwise have to write by hand.

Like:

export type SelectedStates = "" | "IN_PROGRESS" | "SUCCESS" | "ERROR";

// Elsewhere
selectedState?: SelectedStates;

Upvotes: 2

Related Questions