Let's say I have the following:
type Field = {
_id: string;
value: string;
}
const fields = [
{ _id: 'a', value: '2' },
{ _id: 'b', value: '3' }
] as const;
I would like to define a type ValueById<T extends readonly [...Field[]]>
which produces:
{
a: '2',
b: '3',
}
I've gotten partially to where I want to be with this:
export type ValueById<T extends readonly [...Field[]]> = {
[Z in T[number]['_id']]: T[number]['value']
}
type NewType = ValueById<typeof fields>
But unfortunately this produces a type of:
{
a: '2' | '3'
b: '2' | '3'
}
Is there a way to narrow down the type of the values here?