ts踩坑记录
2022-04-26 本文已影响0人
如果俞天阳会飞
错误信息 TS7053
const person = {
name: '张三',
age: 10
};
function getValue(arg: string) {
return person[arg];
}
TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ isMobile: string; isUnion: string; isTelcom: string; no: s
tring; }'.
解决方法1:
在tsconfig.json中配置suppressImplicitAnyIndexErrors: true
{
"compilerOptions": {
"suppressImplicitAnyIndexErrors": true,
...
},
...
}
解决方法2: 给person定义接口
const person = {
name: '张三',
age: 10
};
function getValue(arg: string) {
interface IPerson {
[key: string]: any
}
return (<IPerson>person)[arg];
}
来源: https://blog.csdn.net/lihefei_coder/article/details/103694047