繁体   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