Pesquisa de site

reactjs - Erro no TypeScript em componente com forwardRef onde todas as props são esperadas como boolean


Eu tenho um componente que forma um contêiner para partes da nossa aplicação chamado Box:

interface BoxProps {
  keyId?: number;
  id?: string;
  propsRef?: React.ForwardedRef<HTMLDivElement>;
  span?: number | number[];
  rowSpan?: number | number[];
  center?: boolean;
  textAlign?: string;
  vertAlign?: string;
  transparent?: string;
  bgColor?: string;
  borderColor?: string;
  gridColumnStart?: number;
  gridColumnEnd?: number;
  classList?: string;
  collapsable?: boolean;
  children: React.ReactNode | JSX.Element | Element;
  styleList?: { [key: string]: string };
  title?: string;
  titleTooltip?: string;
  [key: string]: boolean;
}

const Box = forwardRef<HTMLInputElement, BoxProps>(
  (
    props,
    ref
  ) => {
    const {
      keyId,
      id,
      propsRef,
      span,
      rowSpan,
      center,
      textAlign,
      vertAlign,
      transparent,
      bgColor,
      borderColor,
      gridColumnStart,
      gridColumnEnd,
      classList,
      collapsable = false,
      children,
      styleList,
      title,
      titleTooltip
    } = props;

...
return (
<div> stuff </div>

Eu segui este post para determinar como configurar as props: Usando um componente forwardRef com children em TypeScript

Infelizmente, quando tento usar este componente, estou recebendo o erro de que todas as props devem ser booleanas:

// REACT
import React from 'react';
import Box from './Box';
import Description from './Description';

const Recommendations = () => {
    return (
        <Box span={3}>
            <Description data="<h3><img class='margin-right padding-light' src='/assets/nn/images/globe.svg'>Recommendation #1</h3>" />
        </Box>
    )
}

export default Recommendations;

Erro :

webpack compiled with 6 warnings
ERROR in src/components/elements/Recommendations.tsx:8:14
TS2322: Type 'number' is not assignable to type 'boolean'.
     6 | const Recommendations = () => {
     7 |     return (
  >  8 |         <Box span={3}>
       |              ^^^^
     9 |             <Description data="<h3><img class='margin-right padding-light' src='/assets/nn/images/globe.svg'>Recommendation #1</h3>" />
    10 |         </Box>
    11 |     )

ERROR in src/components/elements/Recommendations.tsx:9:13
TS2322: Type 'Element' is not assignable to type 'boolean'.
     7 |     return (
     8 |         <Box span={3}>
  >  9 |             <Description data="<h3><img class='margin-right padding-light' src='/assets/nn/images/globe.svg'>Recommendation #1</h3>" />
       |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    10 |         </Box>
    11 |     )
    12 | }

o que estou fazendo de errado?

Eu esperava nenhum erro de tipo e que ele pegasse os tipos de BoxProps.

Resposta #1

Uma solução foi fazer o typecast do componente e adicionar manualmente o BoxProps

 as React.ForwardRefExoticComponent<BoxProps & React.RefAttributes<HTMLInputElement>>;