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
How to Cast a JSON Object Inside of TypeScript Class? Casting a JSON object to a TypeScript class involves converting a plain JSON object (which lacks methods and proper typing) into an instance of a class that includes all the defined methods and type safety of that class.Types of Objects in TypeScriptPlain Objects: When parsing JSON data using the JS
3 min read
How to map Enum/Tuple to Object in TypeScript ? Mapping enum or tuple values to objects is a common practice in TypeScript for handling different data representations. This article explores various methods to map enumerations (enums) and tuples to objects, providing examples to illustrate each method.Table of ContentManually mapping Enum to Objec
3 min read
How to Require a Specific String in TypeScript Interface ? To require a specific string in the TypeScript interface, we have different approaches. In this article, we are going to learn how to require a specific string in the TypeScript interface. Below are the approaches used to require a specific string in the TypeScript interface: Table of Content Using
2 min read
How to Convert an Array of Objects into Object in TypeScript ? Converting an array of objects into a single object is a common task in JavaScript and TypeScript programming, especially when you want to restructure data for easier access. In this article, we will see how to convert an array of objects into objects in TypeScript.We are given an array of objects a
3 min read
How to Create Arrays of Generic Interfaces in TypeScript ? In TypeScript, managing data structures effectively is crucial for building robust applications. Arrays of generic interfaces provide a powerful mechanism to handle varied data types while maintaining type safety and flexibility. There are various methods for constructing arrays of generic interface
3 min read