TypeScript: 错误 TS7006 — 参数隐式具有 any 类型,如何修复?
大家好!我最近开始学习 TypeScript,遇到了一个无法理解的错误。我有一个简单的代码用于处理用户数组。我试图过滤出活跃用户并输出他们的名字。这是我的代码:
const users = [
{ id: 1, name: '安娜', active: true },
{ id: 2, name: '伊万', active: false },
{ id: 3, name: '玛丽亚', active: true }
];
function getActiveNames(users) {
return users
.filter(user => user.active)
.map(user => user.name);
}
const activeNames = getActiveNames(users);
console.log(activeNames);
当我运行 TypeScript 编译器时,我收到错误:TS7006: Parameter 'users' implicitly has an 'any' type。我不明白这是什么意思。我传递了一个对象数组,TypeScript 应该自己推断出参数的类型。为什么会出现这个错误以及如何修复它?我读到可以在 tsconfig.json 中禁用严格类型化,但这似乎不太对。