Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extends types for section element in Typescript?

In typescript, when our component extends a div we define our props types like this:


import { HTMLAttributes } from "react";

export interface IContainerProps extends HTMLAttributes<HTMLDivElement> {
    // You can add additional props specific to your container component here
    customProp?: string;
  }

However, when the component props extends section element props, I did not find something like this HTMLSectionElement:

In typescript, when our component extends a div we define our props types like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


import { HTMLAttributes } from "react";

// HTMLSectionElement does not exist ?

export interface IContainerProps extends HTMLAttributes<HTMLSectionElement> {
    // other props
  }

>Solution :

You can view the IntrinsicElements in node_modules/@types/react/index.d.ts, to inspect how React types this:

interface IntrinsicElements {
  // ...
  div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
  // ...
  section: React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
  // ...
}

So the type you are looking for is React.HTMLAttributes<HTMLElement>. This type is consistent with the fact that section is primarily used as a semantic tag and contains no unique attributes.

This element only includes the global attributes.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading