I am trying to type my action. Here is the code.
type Increment = {
type: 'INCREMENT'
payload: number
}
const action: Increment = {
type: 'INCREMENT',
payload: 1
}
console.log(action);
Then I wanted to extract 'INCREMENT' into its own variable so I can use it in multiple places. So I did this
const INCREMENT = 'INCREMENT'
type Increment = {
type: INCREMENT
payload: number
}
const action: Increment = {
type: 'INCREMENT',
payload: 1
}
console.log(action);
However the TS compiler is yelling at me and says
'INCREMENT' refers to a value, but is being used as a type here. Did you mean 'typeof INCREMENT'?
so I had to add typeof in front of INCREMENT to make it happy, as in
type Increment = {
type: typeof INCREMENT
payload: number
}
I don't understand why is that when I am using a variable to reference the string suddenly the breaks the rule. Also shouldn't typeof INCREMENT be a string string. I thought it would be equivalent to type: string but apparently here I cannot assign type with anything string other than INCREMENT. Can someone explain this to me?