Typescript - Object Type and Object with examples

In this blog post, We are going to learn the typescript object type with examples.

In the typescript, the object has three variations of creating objects.

  • Object
  • object
  • empty braces

You can check another post on Fix for Object is possibly null

Typescript object type

Typescript introduces a new data type called the object type. This object represents types that are not primitive, such as string, number, void, boolean, null, and undefined.

The reason for introducing other object types is as follows.

JavaScript’s Object class has methods that take an object as a parameter. In Typescript, there is a non-primitive type representation for these methods, which is the object type.

  • Object.getOwnPropertyDescriptor
  • Object.create
  • Object.observe

Passing primitive values to the above methods throws an error at runtime. Typescript gives a compilation error because of the object type assertion check.

object declaration

const obj: object = {};

Typescript Object Class

This is the Object class, which is the core superclass for all JavaScript API classes containing methods like toString and toLocaleString.

  • Typescript Empty Object: Creating Empty Braces

This is a custom object that has an empty object with no variables and methods. An empty object and Object point to the same type.

let myobject = {};
myobject.key = "test"; // error  Property key does not exist on type{}

In the empty object, there is no key associated, It gives a compilation error - Property key does not exist on type.

Only Object also can be created using predefined properties.

Here, we create an object of Emp with two properties: name and id of types string and number, respectively. Typescript treats this object as {name:string, id: number}. When you assign values of different types, it gives a compilation error.

  • Typescript Object Examples
var Emp = {
  name: "Tom",
  id: 1,
};

Emp = {
  name: "Kiran",
  salary: 2000,
};

In the above example, we create an object and then attempt to change the object properties with different values as well as modify its structure. This gives a compilation error, as a new object is not assignable to an existing object.

Objects can be used in many ways, such as:

  • Containing functions including properties
  • Created using object literals
  • Passed as objects to functions
  • Verified using duck typing, which means if the two objects contain the same type and properties.

How to Check if Two Objects are Type-Equal in Typescript?

Typescript’s Reflection API provides a constructor to check object type and returns the ClassName.

Here is an example of checking objects of the same type:

class Parent {}
class Child extends Parent {}
var parent = new Parent();
var child = new Child();
var parent1: Parent;
parent1 = new Child();
console.log(parent.constructor == Parent); // returns true
console.log(child.constructor == Child); // returns true
console.log(parent1.constructor == Child); // returns true
console.log(parent1.constructor == Parent); // returns false