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

177
Visualizações
Avoid initializing every variable inside class's constructor

I have many models ( which are javascript classes ) holding names for my database, and also it's types. Is there a way I can create a new User without having to call all those attributes inside the constructor ?

class User {
    name: string;
    email: string;
    password: string;

    constructor(values: User) {
        this.name = values.name;
        this.email = values.email;
        this.password = values.password;

        // I don't want to call this.name, this.email, ... Since this User model is relatively small, but I have models with hundreds of attributes
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In typescript you can create class fields in the constructor's argument list, like this:

class User {
    // nothing here :)
    constructor(private name: string, private email: string, private password: string) {
        // empty :)
    }
}

If you specify an access modifier, you get the declaration and the assignment automatically.

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