【发布时间】:2021-08-03 17:29:47
【问题描述】:
我在输入数组方法时遇到问题
const person = students.findIndex((student) => student.id === 23)
我得到的第一个错误是括号中的学生元素
const person = students.findIndex((student) => student.id === 23)
TS7006:参数“student”隐含的类型为“any”。
这可以解决
const person = students.findIndex((student:any) => student.id === 23)
这不是很好,所以我尝试
const person = students.findIndex((student:Object) => student.id === 23)
但我明白了
TS2339:“对象”类型上不存在属性“id”
我假设是因为 Object 是通用类型。
这里的最佳做法是什么?我用 filter、map、reduce 做了数百个这样的操作,我必须在 typescript 中定义方法处理的元素吗?
【问题讨论】:
-
你的
students数组有类型吗? -
创建存储在学生数组中的接口
Student。
标签: javascript arrays typescript filter