-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Support Next.js Image in Avatar slots API without TypeScript errors #45443
Comments
Thanks for the feedback, I'll try to find a proper solution for this. |
I open a fix for the What happens at the moment is that the So when user customize the <Avatar
slots={{ img: Image }}
slotProps={{
img: {
blurDataURL: '', // << TS error, `blurDataURL` does not exist
},
}}
/> So far, the simplest workaround I found is to use <Avatar
slots={{ img: Image }}
slotProps={{
img: {
blurDataURL: '',
} satisfies ImageProps,
} as AvatarProps['slotProps']} // << this is required
/> The ideal solution would be that |
+1 I am afraid this will just freeze the TS compiler, especially if we use it on all components for all slots. The other option would be loosening a bit the slot props type, or accepting a generic (we'll need to see if this is better in terms of type check, but I think it should be. |
There are 2 issues, the types of The For So when user customize the slots.img in this case using Next.js Image (has a different props than the default img slot), the slotProps.img will throw an error. <Avatar
slots={{ img: Image }}
slotProps={{
img: {
blurDataURL: '', // << TS error, `blurDataURL` does not exist on the default slotProps.img
},
}}
/>
// `blurDataURL` is a specific prop for the Image (Next.js Image) component OptionsThere are three aspect to consider, "effort", "performance" and "strictness": 1. Dynamically inferring typesThis is the ideal solution, the To achieve this, it's required to update the component type level to sync between Effort: high 2. Provide workaround doc to usersUsing <Avatar
slots={{ img: Image }}
slotProps={{
img: {
blurDataURL: '',
} satisfies ImageProps,
} as AvatarProps['slotProps']} // << this is required
/>
// `blurDataURL` is a specific prop for the Image (Next.js Image) component Effort: low (just update the docs) 3. loosening the types for the slot props with some defaultsUser can use the workaround (2) without casting the type at the end. However, this will affect the users that does not customize the I'd avoid this option at all cost as it does not fix the root cause. A strictness issue will be created by users who does not customize the slots. Effort: medium (require changes to all components with writing specs) ProposalDo (2) immediately, then create a POC for (1). |
Summary
Problem
element, for example:
MUI’s slots API for Avatar allows replacing the default
However, Next.js
<Image>
is not a drop-in replacement for<img>.
It requires additional props (alt, width, height, etc.) and has a different src type(string | StaticImport)
, leading to a TypeScript mismatch when used in theslots.img
prop.For example, this code works at runtime, but throws TypeScript error that
img
andImage
types not match:We can't do the type augmentation either, because then we get an typescript error that
Subsequent property declarations must have the same type
.Workaround
As a temporary workaround I created
AppAvatar
that handles the muiAvatar
and NextImage
integration and added mui Avatar to restricted-imports:It would be good improve mui’s slots API to better support different usecases like Next.js
<Image>
to improve next.js integrationRelated:
#45404
Examples
No response
Motivation
Integrating Next.js
<Image>
with MUI's<Avatar>
is a common use case for Next.js projects that use mui. However, the current slots API does not support it natively, requiring workarounds.Plus generally different solutions may have a slightly different apis, and while they work with mui at runtime, they still produce type errors. It would be really beneficial to have the ability to expand the slot props with custom overrides
Search keywords: Next Image NextImage
The text was updated successfully, but these errors were encountered: