I have a function like this
interface Cat {
color: string,
weight: number,
cute: Boolean, // eventhough all cats are cute!
}
export const doSomething = (
cat: Array<Cat| null>,
index: number,
key: keyof typeof cat,
payload: string | number | Boolean
) => {
....
cat[key] = payload
....
}
This gives me
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
Which I understand is because TypeScript thinks that key can be any string instead one of "color", "weight", "cute".
How would I tell in the function declaration that key is one of the three ("color", "weight", "cute")?
I tried
...
key: keyof Cat,
...
Without luck. This
cat[key] = payload
Gives me now
Type 'string| number | Boolean | ' is not assignable to type '(string & number & Boolean )
catis an array ofCatelements. So its keys are array indices, and thus integers.stringpassed tokeymust be one of thekeys("color", "weight", "cute") defined inCat?