0

Why is the typescript compiler not accepting the following code ? (I tried to have the code self-explanatory).

I get the following errors Type 'Dog' is not assignable to type 'K' and Type 'Drone' is not assignable to type 'K'.

I know I could use a workaround by using a union type Dog | Drone as the output type of my factory but it means updating the signature for each new object created. I was wondering if there was a 'generic' way of declaring this.

interface executeFn<I> {
  (param: I): string
}

interface Robot<I> {
  execute: executeFn<I>
}

interface BaseOrder {
  name: string
}

interface DogOrder extends BaseOrder {
  action: string
}

interface DroneOrder extends BaseOrder {
  destination: string
}

class Dog implements Robot<DogOrder> {
  execute(order: DogOrder): string {
    return 'The action is ' + order.action
  }
}

class Drone implements Robot<DroneOrder> {
  execute(order: DroneOrder): string {
    return 'The destination is ' + order.destination
  }
}

function robotFactory<T extends BaseOrder, K extends Robot<BaseOrder>>(
  param: T
): K {
  if (((param as unknown) as DogOrder).action) {
    return new Dog()
  }
  return new Drone()
}

Note that I also tried to change the generic method signature, but this does not solve the pb.

interface executeFn<I> {
  <T extends I>(param: T): string
}

2 Answers 2

0

The compiler correctly warns you about the fact that K may not always be Dog or Drone.

class DroneAdversary implements Robot<DroneOrder> {
  execute(order: DroneOrder): string {
    return 'The destination is ' + order.destination
  }
}

const adversary = robotFactory<DroneOrder, DroneAdversary>({
  name: 'aaa',
  destination: 'dest'
}); // adversary type is DroneAdversary

I would go with an overload:

function isDogOrder(order: BaseOrder): order is DogOrder {
  return !!((order as DogOrder).action);
}

function robotFactoryOverload(order: DogOrder): Dog
function robotFactoryOverload(order: DroneOrder): Drone
function robotFactoryOverload(order: DogOrder | DroneOrder): Dog | Drone {
  if (isDogOrder(order)) {
    return new Dog()
  } else {
    return new Drone()
  }
}

const x = robotFactoryOverload({
  name: 'aaa',
  destination: 'sasas'
});
3
  • Thx @Lesiak, using the union type Dog | Drone as the return type works, I already mentioned this potential solution but this means updating the signature everytime you add a new type of Robot. I was hoping for a solution where I do not have to do that. Aug 26, 2020 at 8:17
  • 1
    then add a type for the union type Robot = Dog | Drone. You have only one place to update.
    – Lesiak
    Aug 26, 2020 at 8:18
  • And thanks for the type predicate, I did not know we could do that in typescript stackoverflow.com/questions/40081332/… Aug 26, 2020 at 8:50
0

To expand on Lesiak's answer, if you define a Robot type union then it is possible to define all of the overloads automatically which can help if you might have a lot of them.

type AnyRobot = Dog | Drone
type RobotForOrder<O extends BaseOrder, R = AnyRobot> = R extends Robot<O> ? R : never

function robotFactory<T extends BaseOrder> (order: T): RobotForOrder<T> 
function robotFactory<T extends BaseOrder> (order: T): AnyRobot {
  if (isDogOrder(order)) {
    return new Dog()
  } 
  return new Drone()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.