Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
TypeScript: type of class that implements a certain interface or method

Goal

I wanna have a type that describes an object with values of type: Array<ClassRef> where ClassRef is any class that must implement a certain interface.

Approach

type KeyClassMap<SomeUnionType> = {
   [key in keyof SomeUnionType]: Array<ClassRef>
} 

type ClassRef = new (...args: any[]) => any // should extend that interface

interface I {
   [Symbol.hasInstance](hint: string): boolean
}

const _map: KeyClassMap<UnionType> = {
   myKey: [Date, MyOwnClass, MyOtherClass] // <- all these should have implemented Symbol.hasInstance to check with `instanceof` operator
}

Question

The example misses a declaration of a class that implements that interface. I am not sure how to do that.

Each class should only implement this one method ([Symbol.hasInstance]) to check if a value is an instance of that class.

I would like to know how to declare a type of a class that implements that interface or any other way to describe a class that has this method.

I assume that this is not really possible, so I appreciate any other tips.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

When defining your ClassRef type you are really specifying the type of its constructor function. As the constructor function returns an instance of the class you can force the class to implement a specific interface by forcing the return value of its constructor to implement that interface. Your code should look like this:

type ClassRef<T> = new (...args: any[]) => T;

type KeyClassMap<SomeUnionType> = {
   [key in keyof SomeUnionType]: Array<ClassRef<I>>
} 

If you want to use the KeyClassMap type in other places as well, you can of course add another generic to it instead of statically using the I interface.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda