question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with DefaultTheme type definition

See original GitHub issue
  • [ x] The issue is present in the latest release.
  • [ x] I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

When trying to utilize a color from my theme pallete inside a makeStyles, it says that DefaultTheme doesn``t have property ‘palette’, which is odd since I logged the theme object and it has said property. So I went to the type definition of DefaultTheme in node_modules/@material-ui/core/styles/defaultTheme/index.d.ts and notice that the type exported is in fact an empty object:

/**
 * The default theme interface, augment this to avoid having to set the theme type everywhere
 */
export interface DefaultTheme {}

Why should I augment this interface instead of utilizing Theme directly?

Expected Behavior 🤔

No error.

Steps to Reproduce 🕹

https://codesandbox.io/s/modern-rain-h5n3m

Context 🔦

I am using material-ui with Typescript (3.7.5) and I was trying to change the background color of a Divider to a color I have in a custom theme, so I tried to create a class using makeStyles. The problem is that when trying to access the palette property in the theme inside makeStyles, Typescript would give an error (ts 2339) saying that DefaultTheme doesn`t have the palette property.

I have managed to fix the issue in my computer (but not on codesandbox) by importing Theme:

...
import {Theme} from "@material-ui/core";

const useStyles = makeStyles((theme: Theme) => ({
    dividerColor: {
        backgroundColor: theme.palette.secondary.main
    }
}));

Your Environment 🌎

I’m using the tsconfig from create-react-app:

tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

Tech Version
Material-UI v4.10.2
React v16.13.1
TypeScript v3.7.5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
mnajdovacommented, Jun 1, 2021

Note that, @material-ui/styles is no longer part of @material-ui/core. It is a standalone package now that exports some JSS utils (that we will deprecate at some point). If you wish to use it with @material-ui/core’s theme, you need to either:

I would suggest going with option number 1, if you still want to use the utilities exported in @material-ui/styles. I will make sure to include this in the migration guide, as it is most likely going to be a pain for other TS users too.

4reactions
olroscommented, Jun 1, 2021

I’ve experienced the same as @Jack-Works with alpha-v35. Solved it with this augmentation:

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

declare module '@material-ui/styles' {
  // eslint-disable-next-line @typescript-eslint/no-empty-interface
  interface DefaultTheme extends Theme {}
}

Hope this wasn’t intended.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem with DefaultTheme type definition #21551 - GitHub
The problem is that when trying to access the palette property in the theme inside makeStyles, Typescript would give an error (ts 2339)...
Read more >
Property 'palette' is not recognised by DefaultTheme from ...
When I hover over above 'palette' TS give a comment like: Property 'palette' does not exist on type 'DefaultTheme'. Theme is called in...
Read more >
Troubleshooting - Material UI - MUI
[Types] Property "palette", "spacing" does not exist on type 'DefaultTheme'. This error arises because makeStyles is now exported from the @mui/styles ...
Read more >
Create styled.d.ts to make Typescript work with styled ...
Typescript is complaining because the default theme object is just {} . The ThemeProvider injects an object of type DefaultTheme into every prop ......
Read more >
Extend Material-UI theme in TypeScript - In Plain English
It means that because Material-UI package already provide the type declaration of palette options, you can't add extra keys to it.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found