Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input types in Typescript interface

I'm trying to write a component to reuse and one of the varibles is the input type to render.

Currently, I have this:

type InputBaseProps = {
  children?: ReactChild;
  className?: string;
  id: string;
  label?: string;
  name?: string;
  onBlur: (event: ChangeEvent<HTMLInputElement>) => void;
  onChange: (event: ChangeEvent<HTMLInputElement>) => void;
  placeholder?: string;
  style?: CSSProperties;
  type: ????;
  value?: string;
};

I want to allow only valid HTML types, like the entire list defined here https://www.w3schools.com/tags/att_input_type.asp

Is there any simple way to do it?

like image 276
sebazelonka Avatar asked Sep 03 '25 01:09

sebazelonka


1 Answers

import { HTMLInputTypeAttribute } from "react";

type InputBaseProps = {
    type?: HTMLInputTypeAttribute 
}

HTMLInputTypeAttribute resolves to the ff:

type HTMLInputTypeAttribute = "number" | "search" | "button" | "time" | "image" | "text" | "checkbox" | "color" | "date" | "datetime-local" | "email" | "file" | "hidden" | "month" | "password" | "radio" | "range" | ... 5 more ... | (string & {})

Intellisense working fine.

like image 68
neldeles Avatar answered Sep 04 '25 15:09

neldeles