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

173
Vistas
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 Respuestas
Responde la pregunta

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 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