reactjstypescripttypes

Type for basic HTML tags?


I have an exported type called Tags that defines available tags for some components:

export type Tags =
  | 'div'
  | 'article'
  | 'section'
  | 'aside'
  | 'nav'
  | 'figure'
  | 'main'
  | 'header'
  | 'footer';

Obviously, this is something that I've written myself and have to update anytime I want to add a tag; does something like this exist already? Similar to React's HTMLAttributeAnchorTarget?


Solution

  • You can also use HTMLElementTagNameMap. This does not require any imports and it is available in lib.dom.d.ts (included in TypeScript itself).