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...'.
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.
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 {} }
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!
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 !
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.
this worked for me
import { makeStyles } from "@material-ui/styles";
const useStyles = makeStyles((theme: Theme) => ({
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With