Skip to content
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

Type instantiation is excessively deep and possibly infinite (ts-2589) error with styled-component and typescript setup #2984

Open
quangkhaidam93 opened this issue Aug 14, 2024 · 1 comment

Comments

@quangkhaidam93
Copy link

I have an typescript issue Type instantiation is excessively deep and possibly infinite (ts-2589) when using react-svg with styled-component. Please help me resolve this issue. Thank in advance.

My code is below

const BottomNavigation: React.FC<BottomNavigationProps> = ({ buttons }) => {
  return (
    <Container>
      {buttons.map((button) => (
        <Button key={button.id}>
          <Icon
            $active
            src="https://...svg"
          />
          <Label active>{button.label}</Label>
        </Button>
      ))}
    </Container>
  );
};

const Icon = styled(ReactSVG)<{ $active: boolean }>`
  width: 24px;
  height: 24px;

  path {
    fill: ${(props) => (props.$active ? '#FF0000' : '#66798B')};
  }
`;

Some additional image(s):

Screenshot 2024-08-14 at 10 19 37

Screenshot 2024-08-14 at 10 21 17

@quangkhaidam93
Copy link
Author

Temporary solution:

Create ThemeProvider and provide dynamic css variables through style props

return (
    <StyledThemeProvider
      theme={{
        colors: {
          primary: colorConfig?.primary_color ?? '#000000',
          secondary: colorConfig?.secondary_color ?? '#000000',
          headline: colorConfig?.headline_color ?? '#000000',
          pageBackground: '#F5F9FF',
          errorMessage: '#E3173C',
        },
      }}
    >
      <Container
        style={{
          // eslint-disable-next-line @typescript-eslint/ban-ts-comment
          // @ts-ignore
          '--primary-color': colorConfig?.primary_color ?? '#000000',
          '--secondary-color': colorConfig?.secondary_color ?? '#000000',
        }}
      >
        {children}
      </Container>
    </StyledThemeProvider>
  );

Use css variables in Component

css file

.icon {
  width: 24px;
  height: 24px;

  path {
    fill: '#66798B';
  }
}

.icon-active {
  width: 24px;
  height: 24px;
  
  path {
    fill: var(--primary-color);
  }
}

Component file

<ReactSVG
  className={clsx({ 'icon-active': selectedButtonId === button.id, icon: selectedButtonId !== button.id })}
  src={button.icon}
/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant