简体   繁体   中英

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.

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",
};

Image

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM