class Component { }
class ShallowWrapper { }
// creating a TestContainer class with generic types T and P
class TestContainer<T extends Component, P extends object>
{
constructor(reactElement: T, selectors: P) {
// iterating over selectors to define object properties
}
initialize(): void { }
[elem: keyof P]: any
// need to set class properties based on keys of P
// however, encountering a compiler error:
// An index signature parameter type cannot be a union type.
// Suggested solution: use a mapped object type instead.
}
const p = new TestContainer(new Component, { test: 'a' });
const v = p.test // results in a type error (property does not exist)
The code above attempts to dynamically create object properties using generic parameter P. Unfortunately, the compiler throws an error
An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
How can I overcome this issue?