Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: How to declare type list of dictionaries?

I want to declare a list of dictionaries where the key is a string and the value is a number.

How might I modify the current code, see below, that declares count as a a list of strings.

const dict: string[] = [];

like image 962
user19251203 Avatar asked Oct 17 '25 19:10

user19251203


1 Answers

dictionary where the key is a string and the value is a number.

This is a Record type. In this case:

Record<string, number>

Or an index signature would work as well:

{ [myKey: string]: number }

Then just add a [] to the end of either of those to make it an array of those objects.

const dict: Record<string, number>[] = [
  { a: 123, b: 456 },
  { c: 789 },
]
like image 133
Alex Wayne Avatar answered Oct 19 '25 12:10

Alex Wayne