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

286
Views
Typescript - how to pass MyClass.new as method reference?

I have an array of objects, where each object looks like this:

{ id: 'string', name: 'string' }

Given this class:

class User {
  constructor(obj: Record<string, string>) {
    Object.assign(this, obj);
  }

  id: string; 
  name: string;
}

I want to use Array.map() to create an array of User:

const users = userObjects.map(o => new User(o));

The above works, but feels wrong: is there a way to pass a reference to the User's new and avoid the arrow function?

I'm not really sure if the answer is different between Javascript and Typescript (I suspect it isn't), but FWIW I'm coding in Typescript.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No, there isn't (other than a utility function you might write to do that arrow function for you, but that's just an abstraction on what you already have). The issue is that there is no new method (like Java's User::new), there is only the User function itself, and whether it creates a User instance or throws an error (in modern environments) depends on how you call it. If you call it via new (or a similar mechanism like Reflect.construct), it creates a User instance. If you call it directly, it throws an error (in ES2015+; if you're compiling down to ES5, it won't, but it won't work properly either).

What you have (the wrapper arrow function) is a perfectly good way to do it (and probably what I'd do).

If you need to do this in several places, you could put a static method on User:

class User {
    // ...
    static create(...args) {
        return new User(...args);
    }
}
// ...
const users = userObjects.map(User.create);

(Note that create won't work properly in User subclasses [depending on your definition of "properly"], it always creates a User instance, even if called as UserSubclass.create(/*...*/). Normally you could use this instead of User [new this(...args)], which while it looks odd works provided the call sets this correctly [as User.create(/*...*/) or UserSubclass.create(/*...*/) would], but map won't set this correctly so that wouldn't work unless you bind create or remember to pass the second argument to map [.map(User.create, User)], which takes us back to: a wrapper arrow function probably makes more sense.)

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!