The Record type in TypeScript

2021-03-31 #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>;