-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[docs] Passing sx example causes eslint "unsafe spready of 'any' value in an array" error #37730
Comments
It doesn't look like this bug report has enough info for one of us to reproduce it. Please provide a CodeSandbox (https://material-ui.com/r/issue-template-latest), a link to a repository on GitHub, or provide a minimal code example that reproduces the problem. Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve |
@mnajdova Here's a link to a CodeSandbox. My problem with doing a repro is that I can't seem to get CodeSandbox to give eslint errors. Anyway, in PassingSxProp.tsx on line 20 you can see that sx (when it is an array), is of type |
I'm running into the same issue; the recommended way of passing spreading the
which is the actual variant of I couldn't figure out how to get type inference to do the right thing. It seems to me that the the way forward would be for |
Hi everyone, I see there's been some great discussion around the issue with the sx prop type inference when it's an array, especially in relation to the eslint rule 'no-unsafe-assignment'. I'd like to propose a couple of solutions: a quick fix for now and a more robust long-term solution. Quick FixFor an immediate resolution, you can use a custom type guard function instead of `Array.isArray(sx)`. Here's the code snippet: export function isSxArray(
sx: SxProps<Theme>,
): sx is ReadonlyArray<
| boolean
| SystemStyleObject<Theme>
| ((theme: Theme) => SystemStyleObject<Theme>)
> {
return Array.isArray(sx);
} This should help satisfy the eslint rule by providing a more specific type guard. Long-Term SolutionTo provide a more comprehensive and permanent fix, I suggest making some changes to the type declarations and adding a new exported function:
export type SxPropsArray<Theme extends object> = ReadonlyArray<
boolean | SystemStyleObject<Theme> | ((theme: Theme) => SystemStyleObject<Theme>)
>;
/**
* The `SxProps` can be either object or function
*/
export type SxProps<Theme extends object = {}> =
| SystemStyleObject<Theme>
| ((theme: Theme) => SystemStyleObject<Theme>)
| SxPropsArray<Theme>;
export function isSxArray(
sx: SxProps<Theme>,
): sx is SxPropsArray<Theme> {
return Array.isArray(sx);
}
These changes will enhance type safety and address the type loss issue more effectively. I'm more than happy to assist further with these changes. Let me know if you'd like me to proceed with a pull request or if there are any other steps required. Looking forward to your feedback! Best regards, Nayan |
I would also like to request |
Duplicates
Related page
https://mui.com/system/getting-started/the-sx-prop/#passing-the-sx-prop
Kind of issue
Broken demonstration
Issue description
Perhaps the expectation is that users are not prohibiting the use of the
any
type via eslint, but if I copy theListHeader
component example from the referenced docs page, eslint marks it as an error. Specifically, the case wheresx
is an array, eslint sees it as typeany[]
(see screenshot). I'm wondering if there's a way to make this work with the eslint setting to prevent the use ofany
?Context 🔦
Attempting to make a component that accepts an
sx
property and passes it to its first child component (a MUIBox
) and add a few style prop settings. See screenshot.The text was updated successfully, but these errors were encountered: