Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'theme' does not exist when using typescript

I am creating a ReactJS starter project using typescript with material-ui v1.x beta.

The themes as explained at: https://material-ui-1dab0.firebaseapp.com/customization/themes/ do not work. Typescript complains about the property 'theme' not existing. I have been fiddling with the index.d.ts in @types/material-ui replacing the interface 'muiTheme' to 'theme' which results in a whole lot of other errors.

When using 'muiTheme' property instead the errors are gone, however only the default colours are used indicating my custom theme is not used at all.

My code:

import * as React from 'react';
import { MuiThemeProvider } from 'material-ui/styles';
import createMuiTheme from 'material-ui/styles/theme';
import createPalette from 'material-ui/styles/palette';
import { teal, bluegrey, red } from 'material-ui/colors';
import Button from 'material-ui/Button';

const theme: any = createMuiTheme({
  palette: createPalette({
    primary: teal, // Purple and green play nicely together.
    accent: {
      ...bluegrey,
      A400: '#00e677',
    },
    error: red,
  }),
});

function Palette() {
  return (
    <MuiThemeProvider theme={theme}>
      <div>
        <Button color="primary">
          {'Primary'}
        </Button>
        <Button color="accent">
          {'Accent'}
        </Button>
      </div>
    </MuiThemeProvider>
  );
}

Typescript:

(21,23): error TS2339: Property 'theme' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<MuiThemeProvider> & Readonly<{ children?: ReactNod...'.
like image 619
Gerard van der Stelt Avatar asked Aug 27 '17 20:08

Gerard van der Stelt


People also ask

Does the classic 'property does not exist on type window in typescript'?

The code worked fine, but the classic 'Property does not exist on type Window in TypeScript' error flagged up on the build command and TypeScript complained about it endlessly. The Window type is defined in the lib.dom TypeScript module (as per the following documentation on TSDoc.

Where is the type property 'X' on type 'theme'?

Property 'X' does not exist on type 'Theme'. I created types.d.ts file, and added it there, but it doesn't help. import '@emotion/react'; import { themeConfig } from './lib/theme'; declare module '@emotion/react' { export interface Theme extends themeConfig {} }

How to solve'property does not exist on type {}'error?

The "Property does not exist on type ' {}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!

Is it possible to extend material-UI theme in typescript?

Unfortunately, default Material-UI theme is bit limited. So this article explains how you can extend Material-UI theme in TypeScript. Actually, if you master this article, you could extend other types !


2 Answers

For anyone running into a similar error message in 2020, my issue was that I was importing makeStyles from the @material-ui/styles package. Switching to the preferred @material-ui/core/styles package gives you the correct types.

eg. Change:

import { makeStyles } from '@material-ui/styles';

to

import { makeStyles } from '@material-ui/core/styles';

See this github issue discussing the preferred import. I believe the former was introduced with MUI 3 as a temporary shim for some style conversions they were working on.

like image 57
Blaine Garrett Avatar answered Nov 15 '22 08:11

Blaine Garrett


this worked for me

import { makeStyles } from "@material-ui/styles";

const useStyles = makeStyles((theme: Theme) => ({

}));
like image 42
MaxPower Avatar answered Nov 15 '22 10:11

MaxPower