retr0327
retr0327

Reputation: 187

Is there a way to avoid repeating types in Typescript?

Let's say I have an enum:

enum AlertAction {
  RESET = "RESET",
  RESEND = "RESEND",
  EXPIRE = "EXPIRE",
}

I want to create multiple actions, as shown below:

type Action<T> = {
  type: T;
  payload: string;
};

type ActionType =
  | Action<Action.RESET>
  | Action<Action.RESEND>
  | Action<Action.EXPIRE>;

In ActionType, I wrote the code Action<enum> multiple times.

Is there a way to not repeat writing Action<enum>?

Upvotes: 0

Views: 42

Answers (1)

Matthieu Riegler
Matthieu Riegler

Reputation: 54753

It's straightforward :

type ActionType = Action<AlertAction>

Upvotes: 1

Related Questions