[英]Property Does not exist despite null coalescing and optional chaining
I have a piece of code on which I do optional chaining and also null coalescing.我有一段代码,我在上面进行可选链接以及 null 合并。
I don't understand why it still complains about property not existing as shown in the image below我不明白为什么它仍然抱怨属性不存在,如下图所示
The error message is错误信息是
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",
};
The error says 'drawer' does not exist on type '{}'
, that's mean the zIndex
has the type of empty object, you need to define the type of zIndex
inside the theme
globally or use as
to do type assertion.错误提示
'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.