abstract class Passenger {
passengerKey: string;
}
class PassengerAdult extends Passenger {
adultField: string;
}
class PassengerInfant extends Passenger {
infantField: string;
}
class PassengerFactory {
static createPassenger(isInfant: boolean): PassengerAdult | PassengerInfant {
if (isInfant) {
return new PassengerInfant();
}
return new PassengerAdult();
}
}
const passengerInfant: PassengerInfant = PassengerFactory.createPassenger(true);
I get an error that
Type 'PassengerAdult | PassengerInfant' is not assignable to type 'PassengerInfant'. Type 'PassengerAdult' is not assignable to type 'PassengerInfant'. Property 'infantField' is missing in type 'PassengerAdult'.
Can I in some way force typescript to understand what he's returning inside createPassenger
method? I know that in the call line I'd cast the result like
const passengerInfant: PassengerInfant = <PassengerInfant>PassengengerFactory...
but i'd like to avoid that.
Aucun commentaire:
Enregistrer un commentaire