2

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

Sergino
  • 10,128
  • 30
  • 98
  • 159
  • you can't extend an interface you should use implements – RICKY KUMAR Oct 30 '20 at 05:30
  • @RICKYKUMAR if I change `extends` to `implements` got an error `Cannot find name 'implements'` – Sergino Oct 30 '20 at 05:32
  • can you add your full code – RICKY KUMAR Oct 30 '20 at 05:33
  • @RICKYKUMAR just updated with full interface defs – Sergino Oct 30 '20 at 05:36
  • Problem is the assignment of a specific type to a general generic type `T`. As the error message states, `'{ prop1: any; propA: string; }'` is not assignable to type `T` because `T` could also be another sub-type of `Base`. See: https://stackoverflow.com/questions/56505560/could-be-instantiated-with-a-different-subtype-of-constraint-object – Yousaf Oct 30 '20 at 06:46
  • What is a purpose of ```getOjbect``` function? Correct me if I'm wrong. Do you want to create function which able to accept ```Base``` fields, and return these ```Base``` fields with some default values of extended interface? – Николай Гольцев Oct 30 '20 at 09:10
  • @НиколайГольцев yes that is correct. getObject is a function that suppose to return requested type(interface) that based on Base type(interface). So in that case `getObject()` can return `A` and `B` based on that's requested. – Sergino Oct 30 '20 at 23:58

1 Answers1

-1

Looking at your i think you wanted to achieve below case, where you can have a base class which is extended by other classes and you are having a function which returns one of the multiple extended class based on some condition.

interface BaseClass {
  name?: string;
}

interface Class1 extends BaseClass {
  age: string;

}

interface Class2 extends BaseClass {
  address: string;
}

function getBase<T>(name: string): T {
  return {
    name,
    age: '32',
    address: 'Himalaya'
  } as unknown as T;
}

console.log(getBase<Class1>('Ricky').name);
console.log(getBase<Class1>('Ricky').age);

console.log(getBase<Class2>('Ricky').name);
console.log(getBase<Class2>('Ricky').address);
RICKY KUMAR
  • 643
  • 4
  • 11