Explaining this may be a bit tricky, but I'll start with stating my objective and then elaborate on the question as clearly as possible:
Note: The version of typescript doesn't matter to me, any solution would be appreciated.
interface Type {}
interface ExtendedType1 extends Type {}
interface ExtendedType2 extends Type {}
interface Type2 {}
type Lookup<T> = { [key: number]: T } | { [key: string]: T };
// The part causing me trouble
// and the type error that I'm struggling to resolve.
export type DeepKeyMap<TLookupValue, TLookup extends TLookupValue | Record<string, TLookupValue>, TMappedValue> =
| TLookup extends TLookupValue ? TMappedValue : { [key in keyof TLookup]: DeepKeyMap<TLookupValue, TLookup[keyof TLookup], TMappedValue> };
interface ITest<TType extends Lookup<Type[]>> {
data: TType;
}
class Test<TType extends Lookup<Type[]>, TOther extends DeepKeyMap<Type[], TType, Type2>> {
source: ITest<TType>;
mapped: TOther;
constructor(options: ITest<TType>) {}
}
const t = new Test({
data: {
test: {
check: [{} as ExtendedType1]
},
test2: {
check2: [{} as ExtendedType2]
}
}
});
t.mapped.test.check; // <- The expected answer should be Type2
Basically, I am attempting to create a deep mapping of types where one object will determine the keys of another object using as much inference as possible. In the given example, the challenging part for me is the
DeepMap<Lookup<T>, MappedType>
type.
I have searched extensively online for similar examples, but none seem to address nested keys as deeply as I require.
So, I have reached the point of seeking help here. Thank you in advance for any assistance!
Edit: I have consolidated and fixed the example to reflect the current progress of my efforts.