class Component { }
class ShallowWrapper { }
// generic P is a simple object
class TestContainer<T extends Component, P extends object>
{
constructor(reactElement: T, selectors: P) {
// iterate over selectors and define object properties
}
initialize(): void { }
[elem: keyof P]: any
// I need to define class properties based on P's keys
// -- compiler throws --
// An index signature parameter type cannot be a union type.
// Consider using a mapped object type instead.
}
const p = new TestContainer(new Component, { test: 'a' });
const v = p.test // this throws a type error (property does not exist)
In the above code, I'm trying to dynamically define object properties based on generic parameter P. However the compiler throws the error
An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
How do I get around this?