Support Next.js Image in Avatar slots API without TypeScript errors #45443
Labels
component: avatar
This is the name of the generic UI component, not the React module!
customization: dom
Component's DOM customizability, e.g. slot
enhancement
This is not a bug, nor a new feature
typescript
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: