1.遍历数组对象报错
export default function createRouter(routerArr: Array<object>) {
return routerArr.map((item:object) => {
//报错:类型“object”上不存在属性“path”
return <Route path={item.path} Component={item.Component}></Route>
})
}
解决办法:给对象声明一个接口
interface routerObj {
[key: string]: any //报错
}
export default function createRouter(routerArr: Array<object>) {
return routerArr.map((item:routerObj) => {
//不报错
return <Route path={item.path} Component={item.Component}></Route>
})
}
2.又出现了新的报错:Unexpected any. Specify a different type.
解决办法:需要我们手动关闭any警告;在 .eslintrc.cjs中添加新的rules
"@typescript-eslint/no-explicit-any": ["off"]
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
"@typescript-eslint/no-explicit-any": ["off"]
},
这样any就不会报错啦