This particular class is part of my code:
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
}
}
I am utilizing it in the following manner:
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 desire to achieve similar behavior (variable types specified above) but I wish to use identical function names for both prop and obj. My goal is to have an overload that adjusts the returned type based on the property type.
Is this achievable in TypeScript?