I have this interface where I basically want to have an array of hashes. Something like this (propably not correct):
JavaScript
x
5
1
export interface EntitySpec {
2
originId: EntityType;
3
mandatoryProperties: Array<{ [key: string]: string }>;
4
}
5
But I want to apply the interface like this:
JavaScript
1
9
1
const spec: EntitySpec = {
2
originId: 1,
3
mandatoryProperties: {
4
'code': 'sad',
5
'name': 'this',
6
'comment': 'here',
7
},
8
};
9
But I get this: Type ‘{ code: string; }’ is not assignable to type ‘{ [key: string]: string; }[]’. How would I do this properly?
Advertisement
Answer
It’s because mandatoryProperties
is an Array
of objects. Wrap that into []
and you should be fine:
JavaScript
1
11
11
1
const spec: EntitySpec = {
2
originId: 1,
3
mandatoryProperties: [
4
{
5
'code': 'sad',
6
'name': 'this',
7
'comment': 'here',
8
}
9
]
10
};
11