gpt4 book ai didi

javascript - react 全局声明变量获取流类型错误

转载 作者:行者123 更新时间:2023-11-30 19:54:36 25 4
gpt4 key购买 nike

type Props = {
checkedItems:any
};
class MyApp extends Component<Props> {
constructor(props) {
super(props);
this.checkedItems = [];
}
}

在这里,我使用 Flow 对 React 类组件进行类型检查。当我使用 npm run flow 运行此文件时,我收到如下错误无法将数组文字分配给 this.checkedItems,因为 MyApp [1] 中缺少属性 checkedItems。有人能告诉我我应该怎么做才能解决流程错误

最佳答案

在您的代码中,您已经为 Prop 定义了类型,但 checkedItems 不在 Prop 中。您需要为类变量 checkedItems

定义类型
interface IMyAppProps {
prop1: boolean
prop2: string
}
interface IMyAppState {
state1: number
state2: string
}

class MyApp extends Component<IMyAppProps,IMyAppState> {
checkedItems: any //defined your class variable here with any type
constructor(props) {
super(props)
this.checkedItems = []
}
}

关于javascript - react 全局声明变量获取流类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54140719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com