【发布时间】:2022-12-05 23:23:09
【问题描述】:
这是我的命令:
export const roles = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
};
我想像这样按键获取值:
let x = "key1";
let y = roles[x]
我收到了错误
元素隐式具有“任何”类型,因为类型的表达式 'string' 不能用于索引类型 '{ key1: string;键2:字符串; 键3:字符串; }'。没有带有类型参数的索引签名 在类型 '{ key1: string; 上找到 'string'键2:字符串;关键3: 细绳; }'.ts(7053)
我该怎么做?
【问题讨论】:
-
let x: keyof roles = "key1";
标签: typescript