mui / mui/material-ui

Typescript error when using the Type "SlotProps"

Open
#46,031 3 comments 2 reactions 1 assignee View on GitHub

@siriwatknp is already working on this.

Since Apr 28, 2025.

scope: all components typescript waiting for 👍
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Steps to reproduce

Background: we try to utilize the utilities slots function provided in material library, to setup the control level components.

Steps:

  1. please check the live example and look at typescript error
    https://codesandbox.io/p/devbox/mui-slots-forward-ref-type-issue-forked-qyl7t9?file=%2Findex.tsx%3A61%2C30&workspaceId=ws_XjwBjMe1qQqiwHMeyBFRCJ

Type '{ slotProps: { root?: SlotProps<React.ElementType<PaperProps<"div", {}>, keyof React.JSX.IntrinsicElements>, object, CardHeaderProps> | undefined; }; slots: Partial<...> | undefined; }' is not assignable to type '{ component?: ElementType<any, keyof IntrinsicElements> | undefined; slots?: { root?: ElementType<any, keyof IntrinsicElements> | undefined; } | undefined; slotProps?: { ...; } | undefined; }'. The types of 'slotProps.root' are incompatible between these types. Type 'SlotProps<ElementType<PaperProps<"div", {}>, keyof IntrinsicElements>, object, CardHeaderProps> | undefined' is not assignable to type '{ component?: ElementType<any, keyof IntrinsicElements> | undefined; ref?: Ref<any> | undefined; } | ((ownerState: { slots?: Partial<{ root: ElementType<any, keyof IntrinsicElements>; }> | undefined; slotProps?: { ...; } | undefined; sx?: SxProps<...> | undefined; }) => { ...; }) | undefined'. Type 'Partial<Omit<PaperOwnProps & CommonProps & Omit<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "style" | ... 6 more ... | "variant"> & { ...; }, "ref"> & RefAttributes<...>> & SlotCommonProps & object' is not assignable to type '{ component?: ElementType<any, keyof IntrinsicElements> | undefined; ref?: Ref<any> | undefined; } | ((ownerState: { slots?: Partial<{ root: ElementType<any, keyof IntrinsicElements>; }> | undefined; slotProps?: { ...; } | undefined; sx?: SxProps<...> | undefined; }) => { ...; }) | undefined'. Type 'Partial<Omit<PaperOwnProps & CommonProps & Omit<Omit<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & { ...; }, "style" | ... 6 more ... | "variant"> & { ...; }, "ref"> & RefAttributes<...>> & SlotCommonProps & object' is not assignable to type '{ component?: ElementType<any, keyof IntrinsicElements> | undefined; ref?: Ref<any> | undefined; }'. Types of property 'ref' are incompatible. **Type 'LegacyRef<Component<PaperProps<"div", {}>, any, any>> | undefined' is not assignable to type 'Ref<any> | undefined'.** Type 'string' is not assignable to type 'Ref<any> | undefined'.typescript(2322) useSlot.d.ts(65, 5): The expected type comes from property 'externalForwardedProps' which is declared here on type '{ ref: ForwardedRef<any>; } & { className: ClassValue | ClassValue[]; elementType: StyledComponent<MUIStyledCommonProps<Theme> & { ...; }, Pick<...>, {}>; ... 4 more ...; internalForwardedProps?: any; }'

I believe the error come with the conflict of useSlot and SlotProps, which contain LegacyRef and Ref

Current behavior

No response

Expected behavior

No response

Context

No response

Your environment
npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: SlotProps typescript

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.