I do have this class:
class Container<T>
{
prop<K extends keyof T>(key: K): BehaviorSubject<T[K]>
{
return null; // something
}
obj<K extends keyof T>(key: K): Container<T[K]>
{
return null; // something
}
}
And using it like this:
interface Obj
{
id: number;
employee: {
id: number;
name: string;
}
}
let container: Container<Obj>;
let id = container.prop('id'); // BehaviorSubject<number>
let employee = container.obj('employee'); // Container<{ id: number; name: string; }
let employeeName = employee.prop('name'); // BehaviorSubject<string>
Now I want the same thing (type of variables commented above) but I'd like to use the same function name for prop and obj. I want a overload that switchs the returned type base on the property type.
Is it possible in TypeScript?
If I understand you then you can do something like this:
The problem will be of course when using this
Container.get
, you'll need to type assert: