Skip to main content

Index Types

Indexed access — look up a property's type

You can index into a type with Type["key"] to pull out the type of that property — exactly like reading a property from an object, but at the type level.

type Person = {
name: string;
skillLevel: "Beginner" | "Intermediate" | "Expert";
};

// pull the type of the skillLevel property
function printSkillLevel(skillLevel: Person["skillLevel"]) {
console.log(skillLevel);
}

const person: Person = { name: "Ajay", skillLevel: "Expert" };
printSkillLevel(person.skillLevel); // Valid: only the three allowed values pass

If you later change skillLevel's type on Person, printSkillLevel updates automatically — the type stays linked to the source.

Index with keyof to get all value types at once

Indexing by the union of keys (keyof) yields the union of all value types:

const person = {
name: "Ajay",
age: 23,
};

type ValueTypes = (typeof person)[keyof typeof person]; // string | number

Combine with arrays: [number]

Indexing an array/tuple type by number gives the element type:

type Skills = ["Beginner", "Intermediate", "Expert"];
type Skill = Skills[number]; // "Beginner" | "Intermediate" | "Expert"

Indexed access keeps derived types honest: instead of copying a property's type, you reference it. One change at the source updates everything downstream.