Skip to content

Instantly share code, notes, and snippets.

@michalczukm
Last active August 9, 2018 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalczukm/783dca2f3add908af532d978fa66d66f to your computer and use it in GitHub Desktop.
Save michalczukm/783dca2f3add908af532d978fa66d66f to your computer and use it in GitHub Desktop.
Example of using values as types - with type assertion
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