Last active
August 9, 2018 23:20
-
-
Save michalczukm/783dca2f3add908af532d978fa66d66f to your computer and use it in GitHub Desktop.
Example of using values as types - with type assertion
This file contains 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
const Foo = { | |
aFoo: 'SomeAfoo' as 'SomeAfoo', | |
bFoo: 'SomeBfoo' as 'SomeBfoo', | |
cFoo: 'SomeCfoo' as 'SomeCfoo', | |
}; | |
type FooValueTypes = (typeof Foo)[keyof typeof Foo]; | |
function doStuff(value: FooValueTypes) { | |
} | |
// compiles! | |
doStuff('SomeAfoo'); | |
// won't compile! | |
doStuff('Another string'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment