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.

[system] `sx` type error when using `css` with the `sx` property

See original GitHub issue

Duplicates

  • 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
hbjORbjcommented, Jan 10, 2022

@uwinkler

Here is a codesandbox I just created. Please check it out.

1reaction
samesfahani-tuplehealthcommented, Nov 15, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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