🎉 I'm releasing 12 products in 12 months! If you love product, checkout my new blog workingoutloud.dev

Back to home

Diving Into Typescript's Type System: Type Operators

Introduction

TypeScript, a statically typed superset of JavaScript, introduces several advanced type operators that can significantly improve your code's reliability and predictability. Among these powerful tools, keyof, index types, and index signatures stand out for their versatility and utility. In this post, we will deep-dive into these type operators, demonstrating their usage with practical examples that you can try out in the TypeScript Playground.

TypeScript keyof

The keyof operator in TypeScript can be used to create a type representing the property names of another type.

Here's a simple illustration:

type Person = { name: string; age: number; }; type PersonKeys = keyof Person; // "name" | "age"

In the above example, PersonKeys is a type that represents the keys of Person - i.e., name and age.

Now, let's leverage the keyof operator in a function:

function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; } const person: Person = { name: "John", age: 30, }; console.log(getProperty(person, "name")); // "John"

In the getProperty function, T is the type of object, and K extends keyof T, meaning it has to be a key of the object type T. The function returns a property of the object of type T[K].

TypeScript Index Types

Index types are a powerful feature of TypeScript that allow you to create types dynamically based on other types.

Here's a basic example:

type Person = { name: string; age: number; }; type ValueOf<T> = T[keyof T]; type PersonValues = ValueOf<Person>; // string | number

In this example, ValueOf<T> represents the type of values that the properties of T can have. In this case, PersonValues is a type representing any value a Person can have - i.e., string or number.

A practical example involving index types might look like this:

function getValues<T>(obj: T): ValueOf<T>[] { return Object.values(obj); } const person: Person = { name: "John", age: 30, }; console.log(getValues(person)); // ["John", 30]

In the function getValues, it returns an array of values of type ValueOf<T>[], which represents an array of any values that the properties of T can have.

TypeScript Index Signatures

TypeScript index signatures allow you to declare types for object properties with unknown names.

Here's a simple example:

interface StringDictionary { [index: string]: string; } const dict: StringDictionary = { hello: "world", ts: "TypeScript", };

In the above example, StringDictionary is an interface with an index signature that indicates that when a StringDictionary is indexed with a string, it will return a string.

A more complex, practical use-case might involve manipulating a data structure:

interface RecordKeeper<T> { [id: string]: T; } const record: RecordKeeper<Person> = {}; function addRecord( id: string, person: Person, record: RecordKeeper<Person> ): void { record[id] = person; } addRecord("1", { name: "John", age: 30 }, record); console.log(record); // { "1": { name: "John", age: 30 } }

In this example, RecordKeeper<T> is an interface with an index signature. It is a generic type, allowing any type to be stored in the record. The addRecord function adds a Person to the record with a particular ID.

Summary

In TypeScript, keyof, index types, and index signatures are powerful tools that allow you to interact with the structure of types, offering a degree of dynamism and precision to your type definitions. These type operators provide ways to enhance your TypeScript projects, making them more type-safe and robust.

Remember, it's crucial to understand your tools to make the most out of them. So, take the time to explore these concepts, experiment with them on the TypeScript Playground, and watch your TypeScript proficiency grow.

References

Always remember: a TypeScript developer who masters their tools, masters their code. Happy coding!

This post is blog number 12 of 20 for my series on intermediate-to-advance TypeScript tips.

Photo credit: pontebernardo

Personal image

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on Visibuild.

1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.

Diving Into Typescript's Type System: Type Operators

Introduction

Share this post