I normally use Partial<T> interface when making class constructors.
classMyClass{myProp=0;// Default values are applied first.constructor(cfg:Partial<MyClass>={}){extend(this,cfg);// Method that shallow-copies properties from cfg to this, overwriting any defaults.}}
Nice, I didn't know about Partial, nice find, will be useful!
Yup it could be used, but take into account that Object.assign doesn't merge nested objects.
letobjA={b:{name:'Train'}};letobjB={b:{description:'A vehicle that does not like to go uphill :)'}};letmerged=Object.assign(objA,objB);>{b:{description:"A vehicle that does not like to go uphill :)"}}
I normally use
Partial<T>
interface when making class constructors.Interesting pattern. I may have to play with it. Thanks!
Your
extend
method just looks likeObject.assign
:)Completely forgot that one exists. Symptom of working too much with an old IE11-supported javascript framework. :P
Nice, I didn't know about
Partial
, nice find, will be useful!Yup it could be used, but take into account that
Object.assign
doesn't merge nested objects.I do the same except with
Object.assign
with partials like so:Works really nicely for creating and updating data.
Rather than having a web of
Object.assign
everywhere you actually see and work with the shape of your data.