Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
MUI extending button props

I want to extend the MUI5 components and also use their component prop. To do that according to the documentation I make it work in the below code:

import Button, { ButtonProps } from '@mui/material/Button';
import React from 'react';

export type MyButtonProps<C extends React.ElementType> = ButtonProps<C, { component?: C }> & {
  myOtherField: string;
};

export function MyButton<C extends React.ElementType>(props: MyButtonProps<C>) {
  const { myOtherField, ...buttonProps } = props;
  return <Button {...buttonProps}></Button>;
}

However when I try to access anything belongs to MUI5 button attributes, vscode is not helping me to find it see the below screenshot:

But after adding it manually it is not complaining about it. see the below screenshot and check that it is inferring the type as any

However if I remove the <C extends React.ElementType> logic from the type it is working like a charm (but it has a drawback since it is now complaining about the component prop when I use it anywhere in my application):

What can be the problematic part in the first place when I am extending MUI5 button prop types?

thank you in advance.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is the easy way I found for your usecase:

type NewButtonProps = ButtonProps & {
  otherField: number;
  component: React.ElementType;
};

function Button3({ otherField, ...props }: NewButtonProps) {
  const y = props.onClick;
  const x = otherField;

  return <Button {...props}></Button>;
}

All the attributes is understand by typescript now:

work example

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda