TypeScript Indexed Access Types Last Updated : 03 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 object keys.Syntax:type ObjectKeyType = keyof ObjectType;type PropertyType = ObjectType[ObjectKeyType];Where -ObjectType is the object that we want to access the properties of. ObjectKeyType is the type of key that we want to use to access the property. PropertyType is the type of property that we want to access.Example 1: In this example, we define a Book interface with properties like title, author, and year. Using TypeScript's indexed access types, we retrieve the title property's value from a Book instance. JavaScript // Define Book type type Book = { title: string; author: string; year: number; }; // Get type of title property type PropertyType = Book["title"]; // Create Book instance const book: Book = { title: "The Hitchhiker's Guide to the Galaxy", author: "Douglas Adams", year: 1979, }; // Access title property const title: PropertyType = book["title"]; console.log(title); Output:The Hitchhiker's Guide to the GalaxyExample 2: In this example, we define a Book interface and use TypeScript's Indexed Access Types to access and modify the title property. We then update the title value in a Book instance. JavaScript // Define Book type type Book = { title: string; author: string; year: number; }; // Get type of title property type PropertyType = Book["title"]; // Create Book instance const book: Book = { title: "The Hitchhiker's Guide to the Galaxy", author: "Douglas Adams", year: 1979, }; // Change title property book["title"] = "Harry Potter and the Philosopher's Stone"; console.log(book); Output:{ title: "Harry Potter and the Philosopher's Stone", author: 'Douglas Adams', year: 1979} Comment More infoAdvertise with us Next Article TypeScript Indexed Access Types I iamgaurav Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads 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 Higher-Order Types in TypeScript Higher-order types are among the advanced aspects of Typescript that give priority to types as first-class citizens, similar to higher-order functions of JavaScript that accept a function as an argument or return a function, higher-order types can accept types or return types.These are the following 6 min read TypeScript Interfaces Type 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:in 2 min read Opaque Types In TypeScript In TypeScript Opaque types concept allows for the definition of specialized types such as strings or numbers which are derived from primitive types but do not have the characteristics of the base types, the purpose of this is to prevent specific actions regarding the type in question, or to make thi 4 min read Explain the Tuple Types in TypeScript TypeScript is an open-source object-oriented programming language developed and maintained by Microsoft Corporation. It is a strongly typed language and was first introduced in 2012. TypeScript is a strict superset of JavaScript, which means that anything implemented in JavaScript can be implemented 3 min read Like