Is it possible for an editor eg vscode to autosuggest suitable types that conform to a type constraint?

31 Views Asked by At

For the following code, when I've supplied a type parameter to the performAs method of the concrete instance runner is it possible to get my editor to auto suggest easyTask or hardTask since they are the only things in scope assignable to the constraint? Beyond that is it possible within the scope of a project?

eg when I type runner.performAs([ I would like to see easyTask, hardTask

interface Task {
  _type: string;
  perform: () => void;
}

class EasyTask implements Task {
  _type: "easy";
  perform: () => {};
}

class HardTask implements Task {
  _type: "hard";
  perform: () => {};
}

class InvalidTask implements Task {
  _type: "invalid";
  perform: () => {};
}

const easyTask = new EasyTask();
const hardTask = new HardTask();
const invalidTask = new InvalidTask();

type AllowedTasks = HardTask | EasyTask;

class TaskRunner {
  performTasks<T extends Task>(tasks: T[]) {
    tasks.forEach((t) => t.perform());
  }
}

const runner = new TaskRunner();

runner.performTasks<AllowedTasks>([easyTask]);
runner.performTasks<AllowedTasks>([easyTask, invalidTask]); // type error , invalidTask not assignable

0

There are 0 best solutions below