Sebastian De Deyne
Designer & developer at Spatie

The Record type in TypeScript

I can’t count the amount of times I’ve defined an object type with unknown string keys and a specific value type.

type Scores = {
    [key: string]: number;
}

And despite using it all the time, I can’t for the life of me remember the [key: string] syntax.

Today, my problems are solved. Apparently TypeScript has a built in Record type that does exactly that:

type Scores = Record<string, number>;

If you enjoyed this post, you might be interested in my newsletter. I occasionally send a dispatch with personal stories, things I’ve been working on in the past month, and other interesting tidbits I come across online.