Skip to main content

Literal Types

Flow has primitive types for literal values, but can also use literal values as types.

For example, instead of accepting number type, we could accept only the literal value 2.

1function acceptsTwo(value: 2) { /* ... */ }2
3acceptsTwo(2);   // Works!4
5acceptsTwo(3);   // Error!incompatible-typeCannot call acceptsTwo with 3 bound to value because number literal 3 [1] is incompatible with number literal 2 [2].6acceptsTwo("2"); // Error!incompatible-typeCannot call acceptsTwo with "2" bound to value because string [1] is incompatible with number literal 2 [2].

You can use primitive values for these types:

  • Booleans: like true or false
  • Numbers: like 42 or 3.14
  • Strings: like "foo" or "bar"
  • BigInts: like 42n

Using these with union types is powerful:

1function getColor(name: "success" | "warning" | "danger") {2  switch (name) {3    case "success" : return "green";4    case "warning" : return "yellow";5    case "danger"  : return "red";6  }7}8
9getColor("success"); // Works!10getColor("danger");  // Works!11
12getColor("error");   // Error!incompatible-typeCannot call getColor with "error" bound to name because string literal error [1] is incompatible with union type [2].

Consider using Flow Enums instead of unions of literal types, if they fit your use-case.