I need to type the following object structure:
const entities = {
abc: {
id: 'abc'
},
def: {
id: 'def'
}
}
Each object prop key needs to match its corresponding id.
I tried doing this:
interface EntitiesMap<E extends Entity> {
[K in E['id']]: E;
}
interface Entity {
id: string;
}
But this does not ensure that the prop key and id value match. For example:
const entities = {
ghi: {
id: 'aaaaa' // should throw an error as ghi doesn't match aaaaa
}
}
Any ideas how I could make this work?