Can I add a type to a literal in TypeScript? Something like the below:
type State = 'DRAFT' | 'PUBLISHED';
(['DRAFT', 'OOPS']: State[]).map(s => doSomethingWithState(s)); // Should be flagged by the compiler as 'OOPS' is not compatible with State
(['DRAFT']: State[]).map(s => doSomethingWithState(s)); // Valid
(['DRAFT', 'PUBLISHED']: State[]).map(s => doSomethingWithState(s)); // Valid
I have tried using as
but this tells the compiler to treat the as as though it were of the given type, not to check that the literal is of the given type.
I could also do const foo: Type = ...
but I don't need to assign this value, I just want to declare it and use it immediately.