How to specify the type for any class which implements an abstract class?

Viewed 26

Is there a way to specify a type for “any class which implements this abstract class”?

For example:

// LIBRARY CODE
abstract class Table {
  static readonly tableName: string;
}
type TableConstructor = typeof Table;
// type TableConstructor = (new (...args: never) => Table);
const createORM = (tables: Array<TableConstructor>) => {
  return {
      executeQuery() {
        // What type should the "tables" array be
        // to support both of the next two lines
        console.log(tables[0].tableName)
        return new tables[0]();
        //     ~~~~~~~~~~~~~~~~
        // Cannot create an instance of an abstract class
      }
  }
}


// EXAMPLE USER CODE
class Foo implements Table {
  static get tableName() {
      return 'foo';
  }
}
const orm = createORM([Foo]);

TypeScript Playground Example Link

1 Answers
Related