簡體   English   中英

嘗試將 bool 和接口傳遞給組件狀態時,React/Typescript 中的屬性缺少類型?

[英]Property missing type in React/Typescript while trying to pass a bool and interface to component state?

我是 React 和 Typescript 的新手,我找不到將它們傳遞到 React.Componet 狀態的正確方法。 我的代碼仍然按預期工作,但我想知道我做錯了什么!

class App extends React.Component<{}, { isLoading: boolean, thisCoffeeShop: ICoffeeShop }> {
constructor(props: Readonly<{}>) {
    super(props);
    this.state = {
        isLoading: true                    
    }       
}
//....
}

}

我假設它的 Typescript 把我扔了

"Property 'thisCoffeeShop' is missing in type '{ isLoading: true; }' but required in type 'Readonly<{ isLoading: boolean; thisCoffeeShop: ICoffeeShop; }>'."

您所在的州缺少您說會在其中的這家thisCoffeeShop

this.state = {
  isLoading: true   
  thisCoffeeShop: {...} // no idea what structure ICoffeeShop is              
}

如果您不希望啟動時需要此屬性,則可以將該屬性設為可選。

React.Component<{}, { isLoading: boolean, thisCoffeeShop?: ICoffeeShop }>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM