If you're looking to create a constructor with optional parameters and public/private modifiers, you can do so to have a robust model even when calling the constructor without parameters.
class User {
constructor(public id?: any, public name? : any) {}
}
This can be achieved more concisely as:
class User {
constructor(id, name) {
this.id = id;
this.name = name;
}
}
To convert an object into a User instance, you can implement a bind method using Object.assign:
class User {
constructor(public id?: any, public name? : any) {}
bind(object: any): Equipe {
return Object.assign(this, object);
}
}
For a truly solid model, consider using Object.seal within the bind method to prevent dynamic property additions:
class User {
constructor(public id?: any, public name? : any) {}
bind(object: any): Equipe {
return Object.assign(Object.seal(this), object);
}
}
You can now instantiate a User object like this:
let user = new User(); // user = {id: undefined, name: undefined}
and bind it to another object:
let user = new User().bind({id: 1, name: 'bob'})