Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
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 answers
Answer question

0

You can make ILayout a conditional type:

type ILayout = {
    component?: InputType;
    options?: OptionsForInputType
} | {
    component?: DateType;
    options?: OptionsForDateType
}
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!