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

338
Vistas
How to pass a class type as parameter?

As described here: Declare class type with TypeScript (pass class as parameter) i can pass a class type as parameter.

There is a problem:

export namespace customCommand {
    export class Command {
        public constructor(parameter?: any) {

        }
    }

    export function register(command: typeof Command) {
        new command()
    }
}

When i do this

customCommand.register(Map)

or

customCommand.register(Object)

There is no errors. I know that typeof Command and typeof Map both return the same result.

But how can i protect this and pass only Command type?

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

0

In TypeScript, you can construct a new type, something like:

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

Which you can then use this way:

function register(command: Class<Command>) {
  new command()
}

This should constrain TypeScript to ONLY accept classes which construct or extend the Command type.

EDIT:

Sorry, missed something when I evaluated your example. Your class Command is generic enough that almost any constructable object will overlap. What it's missing is constraints.

  1. The constructor will take any first argument, undefined or not, so any class which has a constructor accepting zero or one arguments will match
  2. The signature of the object that will be returned is empty, no methods or properties exist to identify it and so any methods/props will "fall-through" and be allowed as an "extension" of your class

We can over come either of these by modifying either properties or methods required for the Command class, thus constraining derived types to that explicit signature

If we want to narrow the possible matches by adding a method or property specific to Command, then we get the desired behavior you've outlined.

export namespace customCommand {
  export type Class<T> = new (...args: any[]) => T

  export class Command {
    public constructor(parameter?: any) {

    }

    picard (): string {
      return 'Make it so.'
    }
  }

  export function register(command: Class<Command>) {
      new command()
  }
}

/*
  Argument of type 'MapConstructor' is not assignable to
  parameter of type 'Class<Command>'. Property 'picard'
  is missing in type 'Map<any, any>' but required in
  type 'Command'. ts(2345)
*/
customCommand.register(Map)

/*
  Argument of type 'ObjectConstructor' is not assignable to
  parameter of type 'Class<Command>'. The 'Object' type is
  assignable to very few other types. Did you mean to use
  the 'any' type instead? Property 'picard' is missing in
  type 'Object' but required in type 'Command'. ts(2345)
*/
customCommand.register(Object)

/* No error */
customCommand.register(customCommand.Command)

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