What are indexed access types in TypeScript?
An indexed access type reads the type of a property using bracket syntax, like User["email"]. You can use a union of keys, or number to get an array's element type, as in string[][number].
Explain it simply
When you write student["name"] in code, you get the value. When you write Student["name"] in a type, you get the type of that property. It is the same idea, one level up.
This helps when a type is already defined somewhere and you want to reuse a piece of it, say the type of an id field, without copying it. If the original changes, your derived type changes with it.
Example
interface User {
id: number;
email: string;
address: { city: string; pin: string };
}
type Id = User["id"]; // number
type Contact = User["id" | "email"]; // number | string
type City = User["address"]["city"]; // string
// Array element type
type Names = string[];
type Name = Names[number]; // string
// All property types as a union
type AnyValue = User[keyof User];
// number | string | { city: string; pin: string }What to say in the interview
"Indexed access types, T[K], read a property's type from another type. I use them to reuse part of a type, to dig into nested objects, and with [number] to get an array's element type. T[keyof T] gives a union of all the value types."