My interface, Item
, is quite straightforward and is applied to various objects.
This interface mandates that each of these objects must have an assigned itemName
property, but they can also include additional properties with dynamic names if necessary.
To achieve this, I have implemented the following:
interface ItemProperty {
foo: string;
bar: string;
}
interface Item {
itemName: string;
[propertyName: string]: ItemProperty;
}
However, this implementation leads to the error message:
Property 'itemName' of type 'string' is not assignable to 'string' index type 'ItemProperty'.
Since itemName
is technically part of ItemProperty
but it is a string, not an entire ItemProperty
object.
How can I resolve this issue so that itemName
can be set without having to fulfill the requirements of ItemProperty
?
An example of a final instance of Item
could look like the following:
const item: Item = {
itemName: "Item 1",
dynamicProperty1: {
foo: "foo",
bar: "bar"
}
};