TypeScript Property 'XXX' is missing in type '{XXX}'.

来源:4-9 【高阶组件HOC】withAddToCart()

Zhang_Kyra

2021-06-25

图片描述
上面的error,经排查是由于我给props加了类型声明造成的。请问老师,这里的props类型是什么,怎么传递的?
return (props:RobotProps) => {
const setState = useContext(appSetStateContext);

const addShoppingChart = (id, name) => {
写回答

1回答

阿莱克斯刘

2021-06-25

“return (props:RobotProps) => {”出现在你代码第几行?我找不到 

0
1
Zhang_Kyra
老师的代码是没有给props添加类型定义的 export const withAddToCart = (ChildComponent: React.ComponentType) => { // return class extends React.Component {} return (props) => { const setState = useContext(appSetStateContext) const addToCart = (id, name) => { .... 是我在写代码的时候,给props加上了RobotProps类型,这样会引发上面的error。 我猜测是因为在程序启动的过程中,RobotProps中的addShoppingChart还是undefined,只有在程序运行过程中,执行了withAddToCart函数后,addShoppingChart才会有具体的实现。
2021-06-28
共1条回复

React18 系统精讲 结合TS打造旅游电商平台

React18 精讲 + 结合 TS 实战 + 热门业务开发,获取必备技能

1993 学习 · 1015 问题

查看课程