'Force an object to to be indexed by its id on parent dictionary
I have item objects containing an id property, and a dictionary of said items. I want to type it such that each item must be indexed by its id property on the parent dictionary.
I expected this to do the job:
type Item<K extends string = string> = { id: K }
type Dictionary = { [K in string]: Item<K> }
const dict: Dictionary = { abc: { id: 'xyz' } }
I expected this to result in an error, since "abc" !== "xyz", but TypeScript doesn't seem to care. How come? And how do I achieve my goal?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
