How to Cast Object to Interface in TypeScript ?
Last Updated :
23 Apr, 2024
In TypeScript, sometimes you need to cast an object into an interface to perform some tasks.
There are many ways available in TypeScript that can be used to cast an object into an interface as listed below:
Using the angle bracket syntax
You can use the type assertion to cast the object into an interface by defining an interface and using the name of that interface inside angled brackets(<>).
Syntax:
interface interface_name {};
const objectName = {}
const castedInterface = <interface_name> objectName;
Example: The below code example will help you in casting an interface to an object using type assertion.
JavaScript
interface newInterface {
name: string,
desc: string
}
const GFG_Obj: any = {
name: "GeeksforGeeks",
desc: "A Computer Science Portal."
}
const newCastedInterface = <newInterface> GFG_Obj;
Using the as keyword
We can use the as keyword to cast the object into an interface by using the below syntax.
Syntax:
interface interface_name {};
const objectName = {};
const castedInterface = objectName as interface_name;
Example: The below example is an practical implementation of casting an object into an interface using the as keyword.
JavaScript
interface newInterface {
name: string,
desc: string
}
const GFG_Obj: any = {
name: "GeeksforGeeks",
desc: "A Computer Science Portal."
}
const newCastedInterface = GFG_Obj as newInterface;
Using the spread operator
The spread operator syntax can also be used to cast an object into an interface by simply specifying the type of the new object as interface and assign the value of the object using spread operator syntax as shown in below syntax.
Syntax:
interface interface_name {};
const objectName = {};
const castedInterface: interface_name = {...objectName};
Example: The below code example will explain the use of the spread operator to cast an object into an interface.
JavaScript
interface newInterface {
name: string,
desc: string
}
const GFG_Obj: any = {
name: "GeeksforGeeks",
desc: "A Computer Science Portal."
}
const newCastedInterface: newInterface = { ...GFG_Obj };
Using Type Assertion with Object Properties
You can also cast an object to an interface by directly assigning it to a variable of the interface type using type assertion. This method is particularly useful when you have an object with properties that match those defined in the interface.
Syntax:
interface InterfaceName {
property1: type1;
property2: type2;
// Other properties...
}
const objectName: any = {
property1: value1,
property2: value2,
// Other properties...
};
const castedObject: InterfaceName = objectName as InterfaceName;
Example: In this example we initializes an object userData with properties name and age, then asserts it as type User using type assertion (as), potentially risking runtime type mismatches.
JavaScript
interface User {
name: string;
age: number;
}
const userData: any = {
name: "GeeksForGeeks",
age: 22,
};
const user: User = userData as User;
Similar Reads
How to Extract Interface Members in TypeScript ? In TypeScript, you can extract interface members (properties and methods) from a class using several approaches. we are going to learn how to extract interface members in TypeScript. Below are the approaches used to extract interface members in TypeScript: Table of Content Manual Extractionimplement
3 min read
How to check interface type in TypeScript ? Typescript is a pure object-oriented programming language that consists of classes, interfaces, inheritance, etc. It is strict and it statically typed like Java. Interfaces are used to define contacts in typescript. In general, it defines the specifications of an entity. Below is an example of an in
2 min read
How to use Interface with Class in TypeScript ? In TypeScript, interfaces define the structure that classes must adhere to, ensuring consistent object shapes and facilitating type-checking.Interfaces declare properties and methods without implementations, serving as contracts for classes to implement.Classes use the implements keyword to adhere t
3 min read
How to Define Static Property in TypeScript Interface? A static property in a class is a property that belongs to the class itself, rather than to instances of the class. It is shared among all instances of the class and can be accessed without creating an instance of the class. Static properties are defined using the static keyword in front of the prop
3 min read
How to Define Interfaces for Nested Objects in TypeScript ? In TypeScript, defining interfaces for nested objects involves specifying the structure of each level within the object hierarchy. This helps ensure that the nested objects adhere to a specific shape or pattern. Here are step-by-step instructions on how to define interfaces for nested objects in Typ
2 min read