Typescript Tricks: Defining Types from Arrays


Typescript

I recently stumbled across a pretty cool trick you can do in Typescript to define types.

When you have an array like this:

const flags = ["a", "b", "c"] as const;

You can define a type from this like this:

const Flag = (typeof flags)[number];

This snippet basically looks at the type of each element in the array.

© 2025 Marian Lambert