type LemonIndexes = keyof Lemon;
type Lemon = {
attr1: any,
attr2: boolean,
attr3: number
}
...
objects.forEach((obj: { attr__name: keyof Lemon; text__: any; attr__type: any; }) => {
switch (obj.attr__type) {
case "boolean":
let attribute_name : keyof Lemon = obj.attr__name;
lemon[attribute_name] = getBool(obj.text__);
case "int":
let attribute_name : keyof Lemon = obj.attr__name;
lemon[attribute_name] = getInt();
default:
console.log("object type could not be found.");
}
});
getBool(bool : string){ // inside of an object so no function keyword
return bool === 'true';
}
...
I wish to use LemonIndexes as a dynamic variable so I can assign obj[attribute] = value.
If LemonIndexes is only of one type like boolean that works. However, when I introduce a MappedType or a keyof where more than one type is present (see type Lemon), TypeScript throws the following error:
Type 'boolean' is not assignable to type 'never'.ts(2322)
What did I do wrong? Where can I find more information on this issue?
What have you tried? -> I attempted switching out types or using things like LemonIndexesBoolean | LemonIndexesInt or obj: { attr__name , I researched questions on StackOverflow
What were you expecting? -> I was hoping that a Union or Mapped Type would handle this type of scenario