Webstorm 无法识别 ...props 的道具类型
Webstorm doesn't recognize prop type of ...props
我正在声明这样一个函数:
const StudentVideoContainer = ({ course, video, currentScore, storedScore, goal, match, ...props}) => {
其中有几个我不想解构的附加到 props 对象的动作。在我看来,这条链条已经足够长了。然而,prop-types 似乎不愿意承认这些功能,只要我不这样做。
我这样声明我的道具类型:
StudentVideoContainer.propTypes = {
course: PropTypes.shape({
course: PropTypes.shape({}),
sections: PropTypes.array,
}),
video: PropTypes.shape({}),
currentScore: PropTypes.number,
storedScore: PropTypes.number,
goal: PropTypes.number,
props: PropTypes.shape({
getStudentSingleCourse: PropTypes.func,
clearStudentSingleCourse: PropTypes.func,
getStudentVideo: PropTypes.func,
clearStudentVideo: PropTypes.func,
}),
match: PropTypes.shape({
params: PropTypes.shape({
courseId: PropTypes.string,
videoId: PropTypes.string,
})
})
};
StudentVideoContainer.defaultProps = {
course: PropTypes.shape({}),
video: PropTypes.shape({}),
currentScore: PropTypes.number,
storedScore: PropTypes.number,
goal: PropTypes.number,
props: {
getStudentSingleCourse: PropTypes.func,
clearStudentSingleCourse: PropTypes.func,
getStudentVideo: PropTypes.func,
clearStudentVideo: PropTypes.func,
},
match: PropTypes.shape({
params: PropTypes.shape({
courseId: PropTypes.string,
videoId: PropTypes.string,
})
})
};
我尝试将我为 match
放入的所有内容都放在 props
定义中,webstorm 不再将其识别为有效,但是当我将其拉回并对其进行解构时,webstorm 将其识别为有效.
我在控制台中没有收到任何道具未通过验证的错误。如果我将 props
下的任何函数更改为 PropTypes.func 以外的任何函数,我确实会收到一个错误消息,指出函数是预期的,所以我有理由相信它们实际上正在被验证。
我是不是做错了什么?
虽然这在技术上不会产生任何我能看到的错误,但那条红线会让我发疯,我不喜欢下一行抑制评论。直到大约 2 周前,我才知道像这样的道具验证是一回事,所以我认为我只是做错了。
但是 ...props
将不会引入名为 props
的新 属性,在这种情况下,这应该可行:
// Instead of this
props: PropTypes.shape({
getStudentSingleCourse: PropTypes.func,
clearStudentSingleCourse: PropTypes.func,
getStudentVideo: PropTypes.func,
clearStudentVideo: PropTypes.func,
}),
// Use this (same applies for the defaultProps)
getStudentSingleCourse: PropTypes.func,
clearStudentSingleCourse: PropTypes.func,
getStudentVideo: PropTypes.func,
clearStudentVideo: PropTypes.func,
请参阅 this codesandbox 的基本示例(尝试将 "Random" 更改为数字,然后您会在控制台中看到,props 验证是工作)
我正在声明这样一个函数:
const StudentVideoContainer = ({ course, video, currentScore, storedScore, goal, match, ...props}) => {
其中有几个我不想解构的附加到 props 对象的动作。在我看来,这条链条已经足够长了。然而,prop-types 似乎不愿意承认这些功能,只要我不这样做。
我这样声明我的道具类型:
StudentVideoContainer.propTypes = {
course: PropTypes.shape({
course: PropTypes.shape({}),
sections: PropTypes.array,
}),
video: PropTypes.shape({}),
currentScore: PropTypes.number,
storedScore: PropTypes.number,
goal: PropTypes.number,
props: PropTypes.shape({
getStudentSingleCourse: PropTypes.func,
clearStudentSingleCourse: PropTypes.func,
getStudentVideo: PropTypes.func,
clearStudentVideo: PropTypes.func,
}),
match: PropTypes.shape({
params: PropTypes.shape({
courseId: PropTypes.string,
videoId: PropTypes.string,
})
})
};
StudentVideoContainer.defaultProps = {
course: PropTypes.shape({}),
video: PropTypes.shape({}),
currentScore: PropTypes.number,
storedScore: PropTypes.number,
goal: PropTypes.number,
props: {
getStudentSingleCourse: PropTypes.func,
clearStudentSingleCourse: PropTypes.func,
getStudentVideo: PropTypes.func,
clearStudentVideo: PropTypes.func,
},
match: PropTypes.shape({
params: PropTypes.shape({
courseId: PropTypes.string,
videoId: PropTypes.string,
})
})
};
我尝试将我为 match
放入的所有内容都放在 props
定义中,webstorm 不再将其识别为有效,但是当我将其拉回并对其进行解构时,webstorm 将其识别为有效.
我在控制台中没有收到任何道具未通过验证的错误。如果我将 props
下的任何函数更改为 PropTypes.func 以外的任何函数,我确实会收到一个错误消息,指出函数是预期的,所以我有理由相信它们实际上正在被验证。
我是不是做错了什么?
虽然这在技术上不会产生任何我能看到的错误,但那条红线会让我发疯,我不喜欢下一行抑制评论。直到大约 2 周前,我才知道像这样的道具验证是一回事,所以我认为我只是做错了。
但是 ...props
将不会引入名为 props
的新 属性,在这种情况下,这应该可行:
// Instead of this
props: PropTypes.shape({
getStudentSingleCourse: PropTypes.func,
clearStudentSingleCourse: PropTypes.func,
getStudentVideo: PropTypes.func,
clearStudentVideo: PropTypes.func,
}),
// Use this (same applies for the defaultProps)
getStudentSingleCourse: PropTypes.func,
clearStudentSingleCourse: PropTypes.func,
getStudentVideo: PropTypes.func,
clearStudentVideo: PropTypes.func,
请参阅 this codesandbox 的基本示例(尝试将 "Random" 更改为数字,然后您会在控制台中看到,props 验证是工作)