I have an object, which is of type defined there:
interface IObject extends IIndexable {
foo?: string;
foo2?: string;
foo3?: string;
}
My IIndexable looks like this:
interface IIndexable {
[key: string]: string;
}
I want to access its properties using brackets notation like this:
let object: IObject = {};
object["foo"] = "bar";
The problem is that key defined in IIndexable can only be of type of string or number and because my objects' keys are all optional, they are of type string | undefined. Error message I get is:
Property 'fullName' of type 'string | undefined' is not assignable to string index type 'string'.
I cannot make my objects' keys not optional, because my object sometimes can be empty.
I also tried to make objects' type IObject | {}, but that way I still cannot access its properties.
Is there any way to solve this?
extends IIndexablepart?IIndexablein other places where I need to access objects using brackets notation. It's the same as putting[key: string]: string;line at the end of interface where I need this access.