#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
4 min read
Question 41 of 48medium

What are Indexed Access Types?

Accessing nested type properties.

What You'll Learn

  • Type indexing syntax
  • Array element types
  • Nested access

Basic Syntax

code.tsTypeScript
interface User {
  name: string;
  age: number;
  address: {
    city: string;
    zip: string;
  };
}

type Name = User["name"];        // string
type Age = User["age"];          // number
type Address = User["address"];  // { city: string; zip: string }
type City = User["address"]["city"]; // string

Multiple Keys

code.tsTypeScript
type NameOrAge = User["name" | "age"];
// string | number

type AllValues = User[keyof User];
// string | number | { city: string; zip: string }

Array Element Type

code.tsTypeScript
const users = [
  { name: "John", age: 30 },
  { name: "Jane", age: 25 }
];

type User = typeof users[number];
// { name: string; age: number }

// Tuple access
type Tuple = [string, number, boolean];
type First = Tuple[0];   // string
type Second = Tuple[1];  // number

Practical Examples

code.tsTypeScript
interface API {
  users: {
    get: () => User[];
    post: (user: User) => User;
  };
  posts: {
    get: () => Post[];
  };
}

type UsersAPI = API["users"];
type GetUsers = API["users"]["get"];
// () => User[]