简体   繁体   中英

Creating a Generic Dictionary in Typescript

I am trying to create a Generic type that represents a dictionary. Something like this:

export type Dictionary<K extends string | number, V> = {[k: K]: V};

Even though I've limited the type to string | number, I still get an error saying "An index signature parameter type must be either 'string' or 'number'.". Is there any way to achieve this in Typescript?

Record is handy for these kind of situations:

export type Dictionary<K extends string | number, V> = Record<K, V>;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM