簡體   English   中英

盡管 null 合並和可選鏈接,但屬性不存在

[英]Property Does not exist despite null coalescing and optional chaining

我有一段代碼,我在上面進行可選鏈接以及 null 合並。

我不明白為什么它仍然抱怨屬性不存在,如下圖所示

錯誤信息是

TS2339: Property 'drawer' does not exist on type '{}'.
export const AppBar = styled(BaseAppBar, {
  shouldForwardProp: (prop) => prop !== "open",
})<AppBarProps>(({ theme }) => ({
  zIndex: theme?.zIndex?.drawer ?? 2,
}));

AppBar.defaultProps = {
  color: "primary",
};

圖片

錯誤提示'drawer' does not exist on type '{}' ,這意味着zIndex的類型為空 object,您需要在theme內全局定義zIndex的類型或使用as來進行類型斷言。

export const AppBar = styled(BaseAppBar, {
  shouldForwardProp: (prop) => prop !== "open",
})<AppBarProps>(({ theme }) => ({
  zIndex: (theme?.zIndex as { drawer: number }).drawer ?? 2,
}));

暫無
暫無

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

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