How to write a typescript union-like

Is this the right way to write a type that can only take 2 strings? Like a typescript union.

type EitherOne = "one" | "two";
const thatOne: EitherOne = "one";

I’m asking because, I didn’t find this easily.
Is that a type annotation?

type either =
  | @as("ONE") One
  | @as("TWO") Two
let something: either = One
let somethingElse: either = Two

Yes. But string-unions are something that you typically only find in ReScript codebases when dealing with TypeScript libraries. As long as you can stay inside ReScript-Land you would just use

type either = One | Two

Alternatively, you can also use polymorphic variants (denoted by the #). They always compile to strings as long as they don’t have a payload. And the first character does not need to be uppercased.

type either = #one | #two | #"other-number"
let something: either = #one
// "one"
3 Likes