[英]Property 'id' does not exist on type '{} | BoardType'
我在我的 Reactjs ( v18 ) 版本中使用 Typescript 。 我在下面有問題
export interface BoardType {
id: string,
columnOrder: string[] | [],
columns: string[]
}
const [board, setBoard] = useState<BoardType | {}>({});
const ABC = board.id; // Property 'id' does not exist on type '{} | BoardType'
我不知道為什么會發生這個錯誤。 有人可以向我解釋嗎? 謝謝你的支持!
將 setBoard 設置為空的 object 是沒有意義的。 使它成為 null...
const [board, setBoard] = useState<BoardType | null>();
const ABC = board?.id;
您在useState
中給出初始 state: {}
BoardType
有一個 id,但{}
沒有。 所以board
是BoardType | {}
BoardType | {}
可能沒有。
您可以使用可選鏈接,如果board
是{}
,您將得到未定義
像這樣: const ABC = board?.id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.