I'm trying to define a type that yields the type of an object whose keys are provided by a string array and whose types are the types of these keys in provided object type O. However, if a key in the string array is given in the form "column as alias", the key in the yielded object will be the alias, while the type for this key will be the type of the key with name "column" in O.
Here's what I have, but it doesn't compile:
type AliasedObject<O, KA extends string[]> = {
[K in KA[number] extends `${infer C} as ${infer A}`
? A
: K]: C extends keyof O ? O[C] : never;
};
I realize there are a ton of errors, but this code does convey what I'm trying to do. How do I fix it?
UPDATE: It seems like the following should do the trick, but TS isn't recognizing any keys for objects of type AliasedObject:
type SelectedColumnsObject<O, KA extends string[]> = {
[K in KA[number]]: K extends `${infer C} as ${infer A}`
? C extends keyof O
? [A, O[C]]
: never
: K extends keyof O
? [K, O[K]]
: never;
};
type AliasedObject<O, KA extends string[]> = {
[K in KA[number] as SelectedColumnsObject<
O,
KA
>[K][0]]: SelectedColumnsObject<O, KA>[K][1];
};