0

I'm trying to create an typescript from another one. The problem is when I change the property og the cloned object, it changes the property of the source object as well.

Example :

import { arrayProp, ModelType, prop, staticMethod, Typegoose, instanceMethod, InstanceType } from 'typegoose';
class User extends Typegoose
{
    @prop({ required: true })
    name: string;
    @prop({ required: true })
    password: string;
    ...
    @instanceMethod
    renderSafe(this: InstanceType<User>)
    {
        let _return:any = null;

        try
        {
            _return = <any>Object.create(this);
            _return.password = undefined;

            console.log(_return);
            // prints correct object without password property

            console.log(this.password);
            // prints undefined
        }
        catch(e)
        { console.log(e); }

        return _return;
    }
}

Is it possible to clone object so the result could live without affect the source object ? I've also tried using Object.assign({}, this); it leads to the same problem.

T00rk
  • 2,178
  • 3
  • 17
  • 23

0 Answers0