I have a fairly simple interface, Item
, that is assigned to objects.
This interface specifies that each of these objects must have an itemName
property set, but they can also have additional properties with dynamic names, if required.
I've attempted to implement this using
interface ItemProperty {
foo: string;
bar: string;
}
interface Item {
itemName: string;
[propertyName: string]: ItemProperty;
}
Now, obviously this throws the following error:
Property 'itemName' of type 'string' is not assignable to 'string' index type 'ItemProperty'.
As itemName
is technically apart of ItemProperty
, but it is a string
and not an ItemProperty
.
How do I override this, so that itemName
can be set without needing to satisfy ItemProperty
?
A final Item
object might look like the following:
const item: Item = {
itemName: "Item 1",
dynamicProperty1: {
foo: "foo",
bar: "bar"
}
};