PHPnews.io

The Record type in TypeScript

Written by Sebastian De Deyne / Original link on Mar. 31, 2021

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>;

sebastiandedeyne

« Gravatar Images - Honesty »