Skip to content

Generic { new(...args: any[]): T } parameter doesn't work for abstract classes #5843

@jklmli

Description

@jklmli

Following up on #5236 (comment), the proposed solution doesn't cover all bases now that abstract classes are supported.

abstract class A {}

class B extends A {}

function isType<T>(value: any, key: { new(...args: any[]): T }): boolean {
  return value instanceof key;
}

console.log(isType((new B), A))

Error TS2345: Argument of type 'typeof A' is not assignable to parameter of type 'new (...args: any[]) => A'. Cannot assign an abstract constructor type to a non-abstract constructor type.

On a related note, interfaces and abstract classes feel a bit kludgey since I can't seem to verify that an arbitrary object actually conforms to them. I can require them as parameter types and use user-defined type guards, but there's no generic equivalent to instanceof.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions