Skip to content

Instantly share code, notes, and snippets.

@caasi
Created February 26, 2020 14:58
Show Gist options
  • Save caasi/df0025d5bafe204b07efc04fa6569c63 to your computer and use it in GitHub Desktop.
Save caasi/df0025d5bafe204b07efc04fa6569c63 to your computer and use it in GitHub Desktop.
nominal types as enums
type Foo = 0 & { readonly brand?: unique symbol };
type Bar = 1 & { readonly brand?: unique symbol };
type Baz = 0 & { readonly brand?: unique symbol };
type Foobar = Foo | Bar;
const Foobar = {
FOO: 0 as Foo,
BAR: 1 as Bar,
};
const BAZ = 0 as Baz;
function foo(x: Foobar): number {
return x;
}
foo(Foobar.FOO);
foo(Foobar.BAR);
foo(BAZ); // error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment