I have a collection of objects that map other objects with unique identifiers (id
) and names (name
).
My goal is to retrieve the name corresponding to a specific id.
Here is my initial approach:
const obj = {
foo: { id: 1, name: 'one' },
bar: { id: 2, name: 'two', },
baz: { id: 3, name: 'three' },
} as const
type Obj = typeof obj;
type ValueOf<T> = T[keyof T];
type Ids = ValueOf<Obj>['id'];
type GetName<T extends Ids> = (ValueOf<Obj> & { id: T })['name']
type Foo = GetName<1>
However, the Foo
type turns out to be
"one" | "two" | "three"
, instead of just "one"
.
How can I rectify this issue?