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.

Add typings for MaterialUI overrides

See original GitHub issue

Feature Request

Describe the problem related to this feature request

When I’m overriding the Material UI theme, the keys from the MUIDropzone are not available for use.

Describe the solution you’d like

The ability to easily extend the MUI theme overrides type with the overridable props for MUIDropzone.

Describe alternatives you’ve considered

TS-Ignore, but that isn’t going to work well if a new version changes something, and breaks the type safety of typescript. Implementing the change manually by redefining the possible types and redeclaring the Material UI overrides type with them included.

Teachability, Documentation, Adoption, Migration Strategy

A component library named material-ui-pickers does this, and it is easy to extend the overrides for typescript compatibility. This change would only affect Typescript users who wish to override MUI styles, and would be backwards compatible since nothing existed in the past for this functionality which could be broken by this.

Additional context

The following is working for type inspections etc within my Typescript project built within the JetBrains Webstorm IDE:

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

// Interface would be maintained by the repository.
// Of course, this could be split into separate types.
interface MuiDropzoneNameToClassKey {
    MuiDropzoneArea: "root" | "text" | "active" | "invalid" | "textContainer" | "icon";
    MuiDropzonePreviewList: "root" | "imageContainer" | "image" | "removeButton";
    MuiDropzoneSnackbar: "infoAlert" | "successAlert" | "warningAlert" | "errorAlert" | "message" | "icon" | "closeButton";
}

// Overrides would be maintained by the user.

/**
 * Type which accepts anything that's declared above. Can be exported if you wish to define a constant using the types.
 */
type OverridesNameToClassKey = {
    // MaterialUI internally uses the following definition, which also works in my project.
    [Name in keyof MuiDropzoneNameToClassKey]?: Partial<StyleRules<MuiDropzoneNameToClassKey[Name]>>;
};

/**
 * Redeclare the MUI theme override type to allow anything we've defined here to be allowed into the overrides list.
 */
declare module "@material-ui/core/styles/overrides" {
    export interface ComponentNameToClassKey extends OverridesNameToClassKey {}
}

Of course, there may be a better way of achieving the outcome. This is just what I’ve done to make it work.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

1reaction
joeplaacommented, Nov 22, 2021

@AnthonyLzq ow, haha, I didn’t see that. I’ll see if I can fix that rule or ignore it then. Thanks for pointing that out.

0reactions
AnthonyLzqcommented, Nov 22, 2021

These are the exact versions I’m on currently:

"@material-ui/core": "4.12.3"
"@material-ui/icons": "4.11.2"
"@types/node": "16.11.0"
"@types/react": "17.0.30"
"@types/react-dom": "17.0.9"
"material-ui-dropzone": "3.5.0"
"react": "17.0.2"
"typescript": "4.4.4"

I get a different warning this time by the way: An interface declaring no members is equivalent to its supertype.. Can I safely ignore this one?

image

I think you can, since it is an error from eslint error and not from TypeScript. Maybe, your eslint rules have not allowed the use of empty interfaces.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to extend Material UI 3 theme Typescript definition with ...
ts file, Overrides is not an interface but a type (so it won't be extensible this way), which makes me think the Material-UI...
Read more >
TypeScript - Material UI - MUI
You can add static typing to JavaScript to improve developer productivity and code quality thanks to TypeScript.
Read more >
CSS Overrides - Material-UI Pickers
Override styles#. The default pickers appearance is built based on material-ui's theme provided. So pickers will take all colors/fonts/theme settings as any ...
Read more >
Extend Material-UI theme in TypeScript - In Plain English
You can find good docs in Material-UI website. But one difficult thing you encounter is about theme, especially palette. Unfortunately, default ...
Read more >
Global Styling with Material-UI Theme Overrides and Props
Learn how to use global CSS overrides and default props in a theme to customize all instances of a Material-UI component in a...
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