I am tasked with creating a specific object structure, where each object key must match its corresponding ID:
const entities = {
abc: {
id: 'abc'
},
def: {
id: 'def'
}
}
To achieve this, I attempted the following code:
interface EntitiesMap<E extends Entity> {
[K in E['id']]: E;
}
interface Entity {
id: string;
}
However, this code does not guarantee that the object key and ID value will match. In the example below, 'ghi' should throw an error as it doesn't match 'aaaaa':
const entities = {
ghi: {
id: 'aaaaa'
}
}
Does anyone have suggestions on how to ensure the proper matching of keys and IDs?