I have created this type that based on my interfaces:
interface Base {
prop1: any;
}
interface A extends Base {
propA: string;
}
interface B extends Base {
propB: string;
}
And a function that suppose to return any of my interface types
public getObject<T extends Base>(id): T {
return {
prop1: id, //prop on Base type
propA: '2'//prop on type A
}
}
My getObject
throws an error Type '{ prop1: any; propA: string; }' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to '{ prop1: any; propA: string; }'.
How to fix that?
UPDATE:
If I change my function to that it kinda works
public getObject(id): A | B {
return {
prop1: id, //prop on Base type
propA: '2'//prop on type A
}
}
But that means if I add more interfaces that extends Base
I had to add each new interface type to the getObject(id): A | B | ....
return which is weird