Created
February 26, 2020 14:58
-
-
Save caasi/df0025d5bafe204b07efc04fa6569c63 to your computer and use it in GitHub Desktop.
nominal types as enums
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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