Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

340
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda