Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a TypeScript object to a plain object?

Tags:

I'm using a JS library, specifically select2 that acts a tad differently than I'd like if the objects I'm passing it aren't plain objects. This is all checked by using jQuery's isPlainObject function.

Does TypeScript have a cast I'm unaware of that would achieve this without resorting to writing my own?

class Opt {     constructor(public id, public text) {      }      toPlainObj(): Object {         return {             id: this.id,             text: this.text         }     } }  let opts = [     new Opt(0, 'foo'),     new Opt(1, 'bar') ];  console.clear()  console.log('both should be false') $.map(opts, opt => {     console.log($.isPlainObject(opt)) })  console.log('both should be true') $.map(opts, opt => {     console.log($.isPlainObject(opt.toPlainObj())) }) 
like image 362
self. Avatar asked May 18 '16 12:05

self.


People also ask

How do I convert one object to another in TypeScript?

The Typescript generally it converts one data type to another data type by using the cast operation. We can convert all types of datas are converted from one type to another type like that cast object is one of the features for to convert the object type of values to another type.

How do I create a simple object in TypeScript?

Syntax. var object_name = { key1: “value1”, //scalar value key2: “value”, key3: function() { //functions }, key4:[“content1”, “content2”] //collection }; As shown above, an object can contain scalar values, functions and structures like arrays and tuples.

Is everything an object in TypeScript?

Object: It describes the functionality, object helps in representing the non-primitive types that is everything except number, string, boolean, big int, symbol, undefined and null. In TypeScript Object(O uppercased) is different from object(o lowercased).

What is type of object of object in TypeScript?

In TypeScript, object is the type of all non-primitive values (primitive values are undefined , null , booleans, numbers, bigints, strings). With this type, we can't access any properties of a value.


1 Answers

You can use Object.assign():

class Point {     private x: number;     private y: number;      constructor(x: number, y: number) {         this.x = x;         this.y = y;     }      getX(): number {         return this.x;     }      getY(): number {         return this.y;     } }  let p1 = new Point(4, 5); let p2 = Object.assign({}, p1); 

p1 is the class instance, and p2 is just { x: 4, y: 5 }.

And with the toPlainObj method:

class Point {     private x: number;     private y: number;      constructor(x: number, y: number) {         this.x = x;         this.y = y;     }      getX(): number {         return this.x;     }      getY(): number {         return this.y;     }      toPlainObj(): { x: number, y: number } {         return Object.assign({}, this);     } } 

If this is something you need in more classes then you can have a base class which has this method:

class BaseClass<T> {     toPlainObj(): T {         return Object.assign({}, this);     } }  class Point extends BaseClass<{ x: number, y: number }> {     private x: number;     private y: number;      constructor(x: number, y: number) {         super();          this.x = x;         this.y = y;     }      getX(): number {         return this.x;     }      getY(): number {         return this.y;     } } 
like image 160
Nitzan Tomer Avatar answered Nov 16 '22 19:11

Nitzan Tomer