type ZitroneIndexes = keyof Zitrone;
type Zitrone = {
attr1: any,
attr2: boolean,
attr3: number
}
...
objects.forEach((obj: { attr__name: keyof Zitrone; text__: any; attr__type: any; }) => {
switch (obj.attr__type) {
case "boolean":
let attribute_name : keyof Zitrone = obj.attr__name;
zitrone[attribute_name] = getBool(obj.text__);
case "int":
let attribute_name : keyof Zitrone = obj.attr__name;
zitrone[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 want to use ZitroneIndexes as an dynamic var so i can type obj[attribut] = value.
If ZitroneIndexes is only of type lets say boolean that works. But as soon as i add lets say an MappedType or an keyof where more then one type is present(see type Zitrone), typescript throws the following error:
Type 'boolean' is not assignable to type 'never'.ts(2322)
What did i do wrong? Where can i read more about that?
What did you try? -> I did try to switch out types or stuff like ZitroneIndexesBoolean | ZitroneIndexesInt or obj: { attr__name , i reviewed questions on stackoverflow
what were you expecting? -> I expected that an Union or Mapped Type covered that kind of scenario
The answer was to cast the type to boolean like this:
thx for reading this question everyone