I have a type that looks like this:
type Config = {
endpoints: [
{name: "abc", created: false},
{name: "xyz", created: true},
]
}
I have a function that transforms this type into the following:
type Instance = {
abc: {name: "abc", created: false},
xyz: {name: "xyz", created: false},
}
I wrote the following transformation type
type IntersectOf<U extends any> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
type Transform<T extends Config, K extends (keyof T)[] = (keyof T)[]> = IntersectOf<{
[key in keyof K]:
K[key] extends keyof T['endpoints']
// here it says "name" can't be used as index of `T["endpoints"][K[key]]`
? Record<T['endpoints'][K[key]]['name'], T['endpoints'][K[key]]>
: never;
}[number]>;
type Instance = Transform<Config>;
I also tried adding a number type guard, and the error goes away but Instance becomes unknown. Here is the Transform type with number typeguard:
type Trasnform<T extends Config, K extends (keyof T)[] = (keyof T)[]> = IntersectOf<{
[key in keyof K]:
K[key] extends keyof T['endpoints']
? K[key] extends number
? Record<T['endpoints'][K[key]]['name'], T['endpoints'][K[key]]>
: never
: never;
}[number]>;