I'm building a React application and I'm using MUI for my components. I wonder how I can give an !important
property to a style?
I tried this:
<Paper className="left"...>
I'm using withStyles
and WithStyles
. Then in my styles.ts
:
left: { display: "block", float: "left!important", },
But this throws the error:
[ts] Type '"left!important"' is not assignable to type '"right" | "none" | "left" | "-moz-initial" | "inherit" | "initial" | "revert" | "unset" | "inline-end" | "inline-start" | undefined'. index.d.ts(1266, 3): The expected type comes from property 'float' which is declared here on type 'CSSProperties' (property) StandardLonghandProperties<TLength = string | 0>.float?: "right" | "none" | "left" | "-moz-initial" | "inherit" | "initial" | "revert" | "unset" | "inline-end" | "inline-start" | undefined
How would one assign an !important
flag when using material-ui with TypeScript?
Material-UI requires a minimum version of TypeScript 3.2. Have a look at the Create React App with TypeScript example. The strict mode options are the same that are required for every types package published in the @types/ namespace. Using a less strict tsconfig.
By default, MUI components come with Emotion as their style engine. If, however, you would like to use styled-components , you can configure your app by following the styled engine guide or starting with one of the example projects: Create React App with styled-components.
Note: @emotion/react , @emotion/styled , and styled-components are optional peer dependencies of @mui/material , so you need to install them yourself.
You can just cast it. For example:
left: { display: "block", float: "left!important" as any, },
or
left: { display: "block", float: "left!important" as "left", },
Here's a playground example.
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