Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!