Reputation: 12983
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.
Upvotes: 1
Views: 570
Reputation: 1075159
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.)
Upvotes: 3