In my JavaScript object, I have two keys named foo
and bar
.
const x = { foo: '', bar: '' }
I also have a function called abc
that takes a value (which can only be either foo
or bar
).
function abc(value: string) {
const selected = x[value];
}
Currently, the value is of type string. However, I want it to be foo
or bar
(since they are present in the x
object).
I attempted to change it using
function abc(value: typeof x)
But TypeScript does not accept that.
How can I modify my code to achieve the desired functionality?