简体   繁体   English

尽管 null 合并和可选链接,但属性不存在

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

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.错误提示'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.

相关问题 使用无效合并或可选链接进行安全解构 - Safe destructuring using nullish coalescing or optional chaining 属性 &#39;&#39; 不存在于类型 &#39;&#39; - 尽管有接口 - Property '' does not exist on type '' - despite having interface 关于 PhpStorm 中可选链和空合并 JavaScript 运算符支持的问题 - Question about Optional chaining and Nullish coalescing JavaScript operators support in PhpStorm OPTIONAL CHAINING 和 NULLISH COALESCING 运算符的组合未呈现预期结果 - combination of OPTIONAL CHAINING and NULLISH COALESCING operator not rendering the expected result JavaScript 可选链接动态属性 - JavaScript optional chaining dynamic property 是否有带有可选链接/空合并的糖来防止 Typeerrors 与说 Array.map? - Is there sugar with Optional chaining/nullish coalescing to prevent Typeerrors with say Array.map? Eslint 无法解析并以红色高亮显示可选链接 (?.) 和无效合并 (??) 运算符 - Eslint fails to parse and red-highlights optional chaining (?.) and nullish coalescing (??) operators 使用 object 属性访问的可选链接运算符 - Using optional chaining operator for object property access 动态字符串的可选链接作为 JS 中的属性 - Optional chaining for dynamic strings as property in JS 为什么 JavaScript 的可选链使用 undefined 而不是保留 null? - Why does JavaScript's optional chaining to use undefined instead of preserving null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM