Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine object properties in typescript?

Tags:

typescript

I'd like to know the best way to do this, say I have two objects

var objectA = {     propertyA: 1,     propertyB: 2     ...     propertyM: 13 }  var objectB = {     propertyN: 14,     propertyO: 15     ...     propertyZ: 26 } 

If objectC is created by

var objectC = Object.assign(objectA, objectB); 

How can I declare/describe objectC, so the compiler/IDE knows that it has the properties of both objectA and objectB?

I'd like to find a way without the need of defining interfaces for objectA and objectB. I don't want to write declaration and definition/evaluation for the same property twice. This redundancy is significant if I have too many properties on an object.

(Is there an operator that can extract the interface/type of an existing object?)

Is it possible?

like image 639
WawaBrother Avatar asked May 05 '16 04:05

WawaBrother


People also ask

How do you combine properties of two objects?

To merge objects into a new one that has all properties of the merged objects, you have two options: Use a spread operator ( ... ) Use the Object. assign() method.

How do I combine two TypeScript interfaces?

To merge two interfaces with TypeScript, we can use extends to extend multiple interfaces. to create the IFooBar that extends IFoo and IBar . This means IFooBar has all the members from both interfaces inside.

How do you concatenate objects in JavaScript?

The easiest way to merge two objects in JavaScript is with the ES6 spread syntax / operator ( ... ). All you have to do is insert one object into another object along with the spread syntax and any object you use the spread syntax on will be merged into the parent object.


1 Answers

Seems like this should do the trick:

var objectA = {     propertyA: 1,     propertyB: 2,     .     . // more properties here     .     propertyM: 13 };  var objectB = {     propertyN: 14,     propertyO: 15,     .     . // more properties here     .     propertyZ: 26 };  var objectC = {...objectA, ...objectB}; // this is the answer  var a = objectC.propertyA;  var n = objectC.propertyN; 

Based on this article: https://blog.mariusschulz.com/2016/12/23/typescript-2-1-object-rest-and-spread


In addition, the order of the variables in the decomposition matters. Consider the following:

var objectA = {     propertyA: 1,     propertyB: 2, // same property exists in objectB     propertyC: 3 };  var objectB = {     propertyX: 'a',     propertyB: 'b', // same property exists in objectA     propertyZ: 'c' };  // objectB will override existing properties, with the same name, // from the decomposition of objectA var objectC = {...objectA, ...objectB};   // result: 'b' console.log(objectC.propertyB);   // objectA will override existing properties, with the same name, // from the decomposition of objectB var objectD = {...objectB, ...objectA};   // result: '2' console.log(objectD.propertyB);  
like image 56
Alkasai Avatar answered Sep 21 '22 11:09

Alkasai