TypeScript Interfaces Type Last Updated : 16 May, 2024 Comments Improve Suggest changes Like Article Like Report TypeScript Interfaces Type offers an alternative method for defining an object's type, allowing for a distinct naming approach. Syntax:interface InterfaceName { property1: type1; property2?: type2; readonly property3: type3; // ... method1(): returnType1; method2(): returnType2; // ...}Parameters:interface is the keyword itself which is used to declare the intreface.InterfaceName is the name of the interface.property1 is the name of the property in an interface.type1 is the type of the property1 in the interface.method()1 is the method name of the method in the interface.returnType1 is the type of return value of the method()1.Example 1: In this example, one Employee object is defined, and its properties are displayed with in the console output. JavaScript interface Employee { // The readonly-indicates id cannot be modified readonly id: number; firstName: string; lastName: string; age: number; //The ?-indicates city is a optional property city?: string } // Create an object adhering // to the Employee interface const employee1: Employee = { id: 1, firstName: "John", lastName: "Doe", age: 30, }; console.log(`Employee:First Name : ${employee1.firstName} ,Last Name : ${employee1.lastName},Age : ${employee1.age}`); Output: "Employee:First Name : John,Last Name : Doe,Age : 30" Example 2: In this example, a Circle class is implemented, adhering to the Shape interface, and an instance of the circle is created and its color and area are displayed in the console. JavaScript interface Shape { color: string; area(): number; } interface Circle extends Shape { radius: number; } class CircleImpl implements Circle { constructor(public color: string, public radius: number) { } area(): number { return Math.PI * this.radius ** 2; } } const myCircle = new CircleImpl("red", 5); console.log(`Color: ${myCircle.color}, Area: ${myCircle.area()}`); Output: "Color: red, Area: 78.53981633974483" Example 3: In this example, a hybrid interface type for a Product object is defined, and a runtime validation method is added to check object conformity. JavaScript interface HybridProduct { readonly id: number; name: string; price: number; description?: string; validate(obj: any): boolean; } const HybridProduct: HybridProduct = { id: 0, name: '', price: 0, validate(obj: any): boolean { if (typeof obj.id !== 'number' || typeof obj.name !== 'string' || typeof obj.price !== 'number') { return false; } if (obj.description && typeof obj.description !== 'string') { return false; } return true; } }; const product = { id: 1, name: "Laptop", price: 1500 }; console.log(`Product is valid: ${HybridProduct.validate(product)}`); Output: "Product is valid: true" Comment More infoAdvertise with us Next Article TypeScript Interfaces Type julietmaria Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads Interfaces in TypeScript TypeScript is a statically typed superset of JavaScript that adds optional types, classes, interfaces, and other features to help developers build robust and maintainable applications. One of the most powerful features of TypeScript is interfaces, which allow you to define the structure of objects, 4 min read TypeScript Interface vs Type Statements In TypeScript, interface and type are used to define the structure of objects and custom types. While interfaces enforce contracts for classes and support multiple inheritance, types offer flexibility for defining complex and reusable type aliases. Each serves distinct use cases.Table of ContentInte 3 min read TypeScript Generic Types TypeScript Generic Types can be used by programmers when they need to create reusable components because they are used to create components that work with various data types and this provides type safety. The reusable components can be classes, functions, and interfaces. TypeScript generics can be u 2 min read What are TypeScript Interfaces? TypeScript interfaces define the structure of objects by specifying property types and method signatures, ensuring consistent shapes and enhancing code clarity.Allow for optional and read-only properties for flexibility and immutability.Enable interface inheritance to create reusable and extendable 4 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 TypeScript Generic Object Types TypeScript Generic Object Types allow you to create flexible and reusable type definitions for objects. These generic types can work with different shapes of objects while providing type safety, ensuring your code is both robust and adaptable. They are particularly useful for creating functions or c 3 min read TypeScript Object Types TypeScript object types define the structure of objects by specifying property types, ensuring type safety and clarity when passing objects as function parameters.Optional properties, denoted with a ? provide flexibility for objects with varying properties. This approach enhances code robustness by 3 min read Typescript Generic Type Array A generic type array is an array that is defined using the generic type in TypeScript. A generic type can be defined between the angled brackets(<>). Syntax:// Syntax for generic type arraylet myArray: Array<Type>;Example 1: Creating a simple generic type array of number type in Typescri 1 min read Data types in TypeScript In TypeScript, a data type defines the kind of values a variable can hold, ensuring type safety and enhancing code clarity.Primitive Types: Basic types like number, string, boolean, null, undefined, and symbol.Object Types: Complex structures including arrays, classes, interfaces, and functions.Prim 3 min read TypeScript Indexed Access Types TypeScript's Indexed Access Types allow you to access the type of a specific property within an object or array using bracket notation (Type[Key]). This feature provides precise type information about properties or elements, enhancing type safety when accessing deeply nested structures or specific o 3 min read Like