[system] `sx` type error when using `css` with the `sx` property
See original GitHub issueDuplicates
- I have searched the existing issues
Latest version
- I have tested the latest version
Current behavior 😯
In a js
project you can do something like this:
import React from "react";
import ReactDOM from "react-dom";
import Button from "@mui/material/Button";
import { css } from "@mui/system";
const green = css({ color: "primary.400" });
const red = css({ color: "red" });
function App() {
const [flag, setFlag] = React.useState(false);
return (
<Button
onClick={() => setFlag(!flag)}
sx={[flag && green, !flag && red]}
variant="outlined"
>
Hello World
</Button>
);
}
ReactDOM.render(<App />, document.querySelector("#app"));
See: https://codesandbox.io/s/usage-forked-54pr1?file=/index.js
However, if you try the same example as a typescript project, the tsc complains:
No overload matches this call.
Overload 3 of 3, '(props: DefaultComponentProps<ExtendButtonBaseTypeMap<ButtonTypeMap<{}, "button">>>): Element', gave the following error.
Type 'false | SerializedStyles' is not assignable to type 'SystemStyleObject<Theme> | ((theme: Theme) => SystemStyleObject<Theme>)'.
Overload 3 of 3, '(props: DefaultComponentProps<ExtendButtonBaseTypeMap<ButtonTypeMap<{}, "button">>>): Element', gave the following error.
Type 'false | SerializedStyles' is not assignable to type 'SystemStyleObject<Theme> | ((theme: Theme) => SystemStyleObject<Theme>)'.ts(2769)
See https://codesandbox.io/s/usage-forked-vouvj?file=/index.tsx
Expected behavior 🤔
The tsc should not complain
Steps to reproduce 🕹
https://codesandbox.io/s/usage-forked-vouvj?file=/index.tsx
Context 🔦
Thxs for this great project and all you hard work. ❤️
Your environment 🌎
See https://codesandbox.io/s/usage-forked-vouvj?file=/index.tsx
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:9 (5 by maintainers)
Top Results From Across the Web
[system] `sx` type error when using `css` with the `sx` property
Duplicates I have searched the existing issues Latest version I have tested the latest version Current behavior In a js project you can ......
Read more >Why does sx prop typecheck fail? - Stack Overflow
This works for me: import { SxProps } from '@mui/system'; import { Theme } from '@mui/material/styles'; const style: SxProps<Theme> ...
Read more >The `sx` Prop - Theme UI
The sx prop lets you add any valid CSS to an element, while using values from your theme to keep styles consistent.
Read more >The sx prop - MUI System
The sx prop is a shortcut for defining custom styles that has access to the theme. The sx prop lets you work with...
Read more >The Ultimate Guide to the MUI 'sx' Prop - Smart Devpreneur
These are shorthands available in the “MUI System”. Next is border , which is standard CSS. These properties coexist and are equally viable...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@uwinkler
Here is a codesandbox I just created. Please check it out.
I was just about to open an issue about this and came across this as well. Here is my repro working example:
https://codesandbox.io/s/example-material-demo-forked-3xv10?file=/demo.tsx
You can see that the code is “working”, but the type system is not happy.