What if you could ask your code to tell you exactly what type a property has, without guessing or repeating yourself?
Why Indexed access types in Typescript? - Purpose & Use Cases
Imagine you have a big box of different toys, and you want to pick a specific toy by its label every time. Without a clear way to look up the toy by its label, you have to search through the whole box manually each time.
Manually checking each toy's label is slow and can lead to mistakes, like picking the wrong toy or forgetting which labels exist. It's hard to keep track and easy to get confused when the box gets bigger.
Indexed access types let you directly ask for the type of a specific property inside an object type by using its key. This means you can quickly and safely get the exact type you want without guessing or repeating yourself.
type User = { name: string; age: number };
type UserName = string; // manually repeating the typetype User = { name: string; age: number };
type UserName = User['name']; // directly gets the type of 'name'It makes your code smarter and safer by letting you reuse and check types automatically based on existing structures.
When building a form, you can use indexed access types to ensure the input fields match the exact data types defined in your user profile, avoiding mistakes and extra work.
Manual type repetition is slow and error-prone.
Indexed access types let you get property types directly from objects.
This keeps your code clean, safe, and easy to maintain.