[英]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
返回。 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)
But this succeeds.但这成功了。
import { useTheme, Theme } from '@emotion/react';
const theme: Theme = useTheme()
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-theme如https://emotion.sh/docs/typescript#define-a-theme 中所述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.