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

174
Visualizações
React Typescript - Types are incompatible with Thrid Party Library

I have a third party library that export a React Component like this:

// Code I can not change
export default class MyIcon extends React.Component {
...
};

MyIcon.propTypes = {
  color?: PropTypes.String
}

I'm working on Typescript so I made the definition for this module:

// Code I can change
declare module '@thirdparty/MyIcon' {
import React from 'react';

interface Props {
color?: string
}

const Icon: React.FC<Props>;
export default Icon;

Now, there is a typescript Component that has this structure:

// Code I can not change
interface IButton {
icon?: React.Component,
text: string
}

export default const Button: FC<IButton> = ({ icon, text }) => {...}

Everything works good till I want to use the MyIcon component and pass it through the Button component. The error I get is Type React.FC<Props> is incompatible with Component<{}, {}, {}>

I endup doing this:

<Button icon={myIcon as unknown as Component} />

But what it kills me is not knowing why is failing.

ANSWER

After talking with the creator of the third party libraries I found they are having the type as Component because they need to change it internally. But doing this, they force my other third party library to behave as a class and not a ReactNode. So basically typing the Icon will work for one of the Third party libraries but it won't allow me to use the other third party library in my project, See code below:

// Ok Code
<Button icon={MyIcon}>Works</Button>

// Wrong Code
<div><MyIcon /> Error on Typescript</div> 

Clearly the problem was not the type, but the problem is the way one of the third party libs is requiring the Icon to be a Component instead of a ReactNode. This in my case made a whole problem for me. Hope this helps someone.

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

0

Well, you are using React.FC for Icon, but Icon is a class component not a Functional component

try

const Icon: React.Component<Props>
about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of requiring a class-based component, you could just type icon to allow any node:

interface IButton {
  icon?: React.Node,
  text: string
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Here you've said that Icon is of type React.FC<Props>

const Icon: React.FC<Props>;

But here you've said that you expect icon to be either undefined or React.Component.

icon?: React.Component,

React.FC != React.Component.

Change your code to:

icon?: React.FC<Props>

You'd likely need to export that interface:

// icon.tsx

export interface IconProps {
  // types here
}

So that you can use it in external modules.

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