我正在尝试创建表示字典的泛型类型。像这样:
export type Dictionary<K extends string | number, V> = {[k: K]: V};
即使我将类型限制为字符串|数字,我仍然收到一条错误消息:“索引签名参数类型必须为'字符串'或'数字'。” 有没有办法在Typescript中实现这一目标?
答案 0 :(得分:0)
Record
在以下情况下很方便:
export type Dictionary<K extends string | number, V> = Record<K, V>;