简体   繁体   English

打字稿错误说类型上不存在属性

[英]Typescript error says property does not exist on type

Note!笔记! I have managed to mitigate the problem by using "theme: any" in the below declaration but I would prefer a better approach.我已经通过在下面的声明中使用“主题:任何”来缓解这个问题,但我更喜欢更好的方法。

I am using React (v17.0.2) for front-end with material-ui (v5.0.0) and I get the following error:我正在使用 React (v17.0.2) 作为带有 material-ui (v5.0.0) 的前端,但出现以下错误:

Property 'palette' does not exist on type 'Theme'. “主题”类型不存在属性“调色板”。

whenever I try to access my theme like so:每当我尝试像这样访问我的主题时:

import { useTheme } from '@emotion/react';

export default function MyComponent() {

    const theme = useTheme()

    return (
        <Box
            sx={{
                backgroundColor: theme.palette.mode === 'dark' ? 'primary.dark' : 'primary',
            }}
        ></Box>
    );
}

I logged the object with console.log(theme) underneath the declaration, and it logged my theme succesfully.我在声明下面用console.log(theme)记录了对象,它成功地记录了我的主题。 So it is there but I cannot access it like shown above.所以它在那里,但我无法像上面显示的那样访问它。 Here is some of what was logged:以下是记录的一些内容:

{breakpoints: {…}, direction: 'ltr', components: {…}, palette: {…}, spacing: ƒ, …}
  > breakpoints: {keys: Array(5), ...}
  > components: {MuiTypography: {…}, ...}
  direction: "ltr"
  > mixins: {toolbar: {...}}
  > palette: {mode: 'dark', ...}
  ...

Also, I found the file where the type "Theme" is located and the property "palette" definitely exists.另外,我发现类型“主题”所在的文件和属性“调色板”肯定存在。 Here is a snippet of the file:这是文件的一个片段:

export interface Theme extends SystemTheme {
  mixins: Mixins;
  components?: Components;
  palette: Palette;
  shadows: Shadows;
  transitions: Transitions;
  typography: Typography;
  zIndex: ZIndex;
  unstable_strictMode?: boolean;
}

I also tried importing and using Theme like so:我也尝试像这样导入和使用 Theme:

import { Theme } from '@mui/material/styles';
...
const theme: Theme = useTheme()
...

And this gave me a new error ( underlining the "theme" variable ):这给了我一个新错误(在“主题”变量下划线):

Type 'Theme' is missing the following properties from type 'Theme': mixins, palette, shadows, transitions, and 6 more. “主题”类型缺少“主题”类型中的以下属性:混合、调色板、阴影、过渡等 6 个。

I tried like this too:我也这样试过:

import { Theme } from '@mui/material/styles';
...
const theme = useTheme<Theme>()
...

And this gave me a new error ( underlining "Theme" in useTheme< Theme >() )这给了我一个新的错误(在 useTheme< Theme >() 下划线“Theme”)

Expected 0 type arguments, but got 1.预期 0 个类型参数,但得到 1 个。

and also并且

Property 'palette' does not exist on type 'Theme'. “主题”类型不存在属性“调色板”。

I am new to typescript, so any help is appreciated.我是打字稿的新手,因此感谢您的帮助。

EDIT Got the answer thanks to Alex Wayne (and maybe also windowsill if I initially misunderstood the answer).编辑 感谢亚历克斯韦恩得到了答案(如果我最初误解了答案,也许还有窗台)。 Here's the code that worked:这是有效的代码:

import { useTheme, Theme } from '@mui/material';
const theme: Theme = useTheme()
<Box sx={{backgroundColor: theme.palette.mode}}></Box>
  • @emotion/react exports a Theme type, returned by useTheme() from the same package. @emotion/react导出一个Theme类型,由useTheme()从同一个包中返回。
  • @mui/material/styles exports a Theme type, returned by createTheme from the same package. @mui/material/styles导出一个Theme类型,由同一个包中的createTheme返回。
  • These are not the same type.这些不是同一类型。

They have the same name in each package, but are completely unrelated.它们在每个包中都具有相同的名称,但完全不相关。

This is why this fails:这就是失败的原因:

import { useTheme } from '@emotion/react';
import { Theme } from '@mui/material/styles';
const theme: Theme = useTheme()
// Type 'Theme' is missing the following properties from type 'Theme': mixins, palette, shadows, transitions, and 2 more.(2740)

Playground 操场

But this succeeds.但这成功了。

import { useTheme, Theme } from '@emotion/react';
const theme: Theme = useTheme()

Playground 操场

I don't know exactly which one you intend to use, but here are the docs on emotion themes and here are the docs on Material UI themes .我不知道您打算使用哪一个,但这里有关于情感主题的文档这里有关于 Material UI 主题的文档 They are separate things, and you need to use each according to their intended use.它们是独立的东西,您需要根据它们的预期用途来使用它们。

In order to get proper type checks you can extend emotion Theme interface with MUI's.为了获得正确的类型检查,您可以使用 MUI 扩展情感主题界面。

import { Theme as MuiTheme } from "@mui/material/styles";

declare module '@emotion/react' {
  export interface Theme extends MuiTheme {}
}

As specified in https://emotion.sh/docs/typescript#define-a-themehttps://emotion.sh/docs/typescript#define-a-theme 中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿类型错误属性不存在 - Typescript type error property does not exist Typescript 属性在类型“{}”上不存在 - Typescript property does not exist on type '{}' 类型 [] 打字稿上不存在属性 - property does not exist on type [] typescript 属性在类型上不存在-TypeScript - Property does not exist on type - TypeScript 如何解决Typescript中“ object”类型上不存在的“属性”字符串问题 - How To fix 'Property “string” does not exist on type “object”' error in Typescript 在Typescript Reac + Redux中具有联合类型的类型错误中,属性不存在 - Property does not exist on type error with Union Types in Typescript Reac + Redux "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }' TypeScript:如何解决 arrays 的“类型不存在属性”错误? - TypeScript: How to solve 'property does not exist for type' error for arrays? TypeScript 错误:类型“Window”上不存在属性“X” - TypeScript error: Property 'X' does not exist on type 'Window' NextJS/Typescript/Apollo 错误; 类型上不存在属性 - NextJS/Typescript/Apollo error; property does not exist on type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM