Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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. 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:

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.

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>() )

Expected 0 type arguments, but got 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>
like image 663
Sean Avatar asked Feb 20 '26 19:02

Sean


1 Answers

In order to get proper type checks you can extend emotion Theme interface with MUI's.

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

like image 136
Wunsz Avatar answered Feb 22 '26 11:02

Wunsz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!