Skip to content
loading...

re: Typescript Partial<T>, where have you been my whole life? VIEW POST

TOP OF THREAD FULL DISCUSSION
re: I normally use Partial<T> interface when making class constructors. class MyClass { myProp = 0; // Default values are applied first. ...
 

I do the same except with Object.assign with partials like so:

export class User {
    id: number;
    name: string;
    profile = new Profile();

    constructor(options?: Partial<User>) {
        Object.assign(this, options);
    }
}

Works really nicely for creating and updating data.

onChangeUser(event: FormChangeEvent<UserForm>): void {
    this.user = new UpsertUser({
        ...this.user,
        ...event.value,
        profile: new Profile({
            ...this.user.profile,
            ...event.value.profile
        })
    });
}

Rather than having a web of Object.assign everywhere you actually see and work with the shape of your data.

code of conduct - report abuse