How can the User class be extended in TypeScript using Partial<User>
as the constructor?
I am open to alternative solutions that do not involve Partial
. For instance, initializing a blank class with new User({})
Currently, the AdvancedUser
class only inherits properties from the User
class, without any additional advanced?:
properties.
export class User {
first_name: string = ''
last_name: string = ''
email: string = ''
constructor(data: Partial<User>) {
Object.assign(this, data)
}
}
export class AdvancedUser extends User {
advanced?: {
foo?: string
}
constructor(data: Partial<User>) {
super(data)
}
}
The code provided is functional. An error in my project caused the AdvancedUser()
call to revert back to User()
.