Closed
Description
Problem
During object declaration, there is no way to explicitly define a property literal type:
const obj = {
hello: 'world'
}
Here typeof obj
is { hello: string }
Example of Use Case
In the case of a Redux Action, which will be constant, I expect type
property to be a literal type:
const increment = {
type: 'INCREMENT'
}
const decrement = {
type: 'DECREMENT'
}
export type Action =
& typeof increment
& typeof decrement
Current Workaround
It's possible to explicitly define the literal type after the value:
const increment = {
type: 'INCREMENT' as 'INCREMENT'
}
Which causes two problems:
- redundancy
- misspelling won't be analysed (
'foo' as 'bar'
will work)
readonly
would be a solution, but does not seem to be compatible with object declaration syntax.