I am currently working with an object that contains keys as strings and values as strings. Here is an example of how it looks:
const colors = {
red: '#ff0000',
green: '#00ff00',
blue: '#0000ff',
}
Next, I define a type that only accepts the keys of this object. For instance:
// Output: 'red' | 'green' | 'blue'
type ColorType = keyof typeof colors
Now, my goal is to utilize this type to validate a value from a function that retrieves a value from the colors
object. However, I encounter an error when attempting to do so. Here is an example:
const getColor = (color: ColorType): ColorType => {
// This error occurs: Type 'string' is not assignable to type '"red" | "green" | "blue"'
return colors[color]
}
How can I overcome this issue?