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

180
Visualizações
React abstract component (ES6) that renders an array of components. How to create a d.ts file for it?

I am trying to create a declaration file for the next component. It was written with JS ES6.

function Column({ className, layout }) {
    function renderLayout() {
        return layout.map((item, index) => {
            const Component = item?.component || item;

            return (
                <Component
                    {...item?.options}
                    // eslint-disable-next-line react/no-array-index-key
                    key={index}
                />
            );
        });
    }

    return (
        <div className={className} >
            {renderLayout()}
        </div>
    );
}

Example of declaration:

<Column
    className='test'
    layout={[
        {
            component: Input, // It can use options
            options: {  // It can have a custom shape. How to define types for options?
                first: 'test' 
            }
        },
        {
            component: Date, // It can use options
            options: { // It can have a custom shape. How to define types for options?
                second: 'test', 
                newValue: { index: 3 } 
            }
        }
    ]}
/>
<Column
    className='test'
    layout={[Input, Date]}
/>

I have a problem with options property. I do not know how to define types for the object as it can have different types depending on the component the user pass with.

Input, Date - are examples of components, it can be replaced with any other components.

My declaration file.

interface ILayout {
    component?: React.ElementType;
    options?: any; // <---- Here is a problem. How to replace any with defined types.
}

interface ColumnProps {
    className?: string;
    layout?: (ILayout|React.ElementType)[];
}

export class Column extends React.Component<ColumnProps> {} 
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You can make ILayout a conditional type:

type ILayout = {
    component?: InputType;
    options?: OptionsForInputType
} | {
    component?: DateType;
    options?: OptionsForDateType
}
about 4 years ago · Santiago Gelvez 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