Reputation: 37584
Typescript allows us to use string literals as types. The question is can I reference them with the dot-notation? For example I have a type like this
export type SomeTypes = 'OPEN' | 'CLOSED' | 'PROGRESSED' | 'DONE';
Can I somehow use it like this?
if(typecheck === SomeTypes.OPEN)
Right now I have to write out the string literal which is not entirely the type safety way. It looks like this
if(typecheck === 'OPEN')
Upvotes: 0
Views: 628
Reputation: 7820
Have a look at TypeScript 2.4 - it supports String Enumerations.
Apart from that (since 2.4 introduces breaking changes) a more expressive "workaround" is as follows - define the constants as variables and then provide the alias upon the constants:
export const SOME_TYPE_OPEN = 'OPEN';
export const SOME_TYPE_CLOSED = 'CLOSED';
...
export type SomeType = SOME_TYPE_OPEN | SOME_TYPE_CLOSED | ...;
Then you can use:
function getIt(value: SomeType) {
if (SOME_TYPE_OPEN === value) { ... }
...
}
Upvotes: 1