I keep encountering the eslint warning "@typescript-eslint/no-unsafe-assignment". This warning occurs when I am trying to extract specific keys and values from a variable that can be assigned objects of different types, with approximately 10 possible classes.
// var **key** can be any property of object1 and depending on which property
// the result will be an object that is of multiple different types
Object.keys(object1).forEach((key) => {
const object2 = object1[key]; // depending on key this can be of type class A, B, C, D and so on
Object.getOwnPropertyNames(object2).forEach((property) => {
// ESLINT error because linter does not know the type of object2
// which could be multiple classes depending on key
const var1 = object2[property]
I attempted a solution but it didn't work as expected. How can I handle this scenario where I am uncertain about the type of my object because I am iterating through them dynamically.
// properties with **class type** values
const objectTypes = {
first: A,
second: B,
third: C,
fourth: D,
}
....
....
// Then based on the property that is the same value as the key to get object2
// I get the correct type from my objectTypes
const var1Type = objectTypes[key];
const var1 = (object2 as var1Type)[property];