lundi 22 juin 2020

Method overriding in Typescript with different return types without hack

I am trying to use a class adapter pattern in typescript

class Source {
    getSomething(): TypeA
}

class Adapter extends Source {
   getSomething(): TypeB {
     const response = super.doSomething();
     // do something with the shape of response
     return response;
   }
}

Typescript is complaining that these two types are incompatible. I am not sure if this is something doable in Typescript without typehint any or union TypeA | TypeB. Imagine a scenario where I want the client code to be able to use both the Source and Adapter class and still be able to get proper type checks.

Thanks

Aucun commentaire:

Enregistrer un commentaire