在 TS 中使用对象遍历时,下角标不允许是字符串。
for (let k in obj) {
let s = obj[k];
}
1
2
3
2
3
报错:
Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type ‘{ ..... }’.
No index signature with a parameter of type ‘string’ was found on type ‘{..... }’.ts(7053)
解决办法为修改tsconfig.json
,允许索引
"compilerOptions": {
//...
"suppressImplicitAnyIndexErrors": true
},
1
2
3
4
2
3
4