Hi everyone. :)
As a followup to this question a have the following code:
interface SubThing {
name: string;
}
interface Thing {
subThing: SubThing; // or any other property of type SubThing like in UpperThing
}
interface UpperThing {
thing: Thing
}
interface ThingMapA {
map: { thing: { subThing: { name : 'subThing name' } } };
}
interface ThingMapB {
map: { thing: { otherThing: { name : 'subThing name' } } };
}
class Handler<T extends Record<keyof T, Record<keyof T, Thing>>> {}
const handler = new Handler<ThingMapA>(); // error
const handler = new Handler<ThingMapB>(); // error
I world like Handler
to accept any class with properties (ideally at least one) of type UpperThing
which has any property (ideally at least one) of type Thing
. But ThingMap
is not recognised. It leads to an error. Any suggestions?
This is another example based on the approach of @jcalz
interface SubThing {
name: string;
}
interface Thing {
subThing: SubThing;
}
interface UpperThing {
thing: Thing
}
interface ThingMapA {
map: { thing: { subThing: { name: 'subThing name' } } };
}
interface ThingMapB {
map: { otherThing: { subThing: { name: 'subThing name' } } };
}
interface ThingMapC {
map: { thing: { otherSubThing: { name: 'subThing name' } } };
}
interface ThingMapD {
map: { otherThing: { otherSubThing: { name: 'subThing name' } } };
}
class Handler<T extends { [K in keyof T]: { [P in keyof T[K]]: Thing } }> { }
const handlerA = new Handler<ThingMapA>(); // okay
const handlerB = new Handler<ThingMapB>(); // okay
const handlerC = new Handler<ThingMapC>(); // error
const handlerD = new Handler<ThingMapD>(); // error