1

I have many parts in my code where I define specific types like this:

function foo (state: "open" | "closed") { ... }

I want to know if there is a way to define that type like interfaces. something like:

type State: "open" | "closed";

so I can use it in a clearer way:

function foo (state: State) { ... }

ramonchi
  • 13
  • 4

1 Answers1

0

You can use type. You are almost there:

type State = "open" | "closed";

function foo (state: State) { console.log(state); }
foo("open"); 
foo("openx"); //Error
foo("closed");

Playground Link

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39