Skip to content

Instantly share code, notes, and snippets.

@renerbaffa
Last active September 4, 2023 15:24
Show Gist options
  • Save renerbaffa/b0177eeb3566db34e6782010592c7715 to your computer and use it in GitHub Desktop.
Save renerbaffa/b0177eeb3566db34e6782010592c7715 to your computer and use it in GitHub Desktop.
What to extend in order to support spread props with typescript
interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
icon: React.ReactNode
// ...other props
}
interface CustomDivProps extends React.HTMLAttributes<HTMLDivElement> {
content: string
// ...other props
}
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
valid?: boolean
// ...other props
}
interface SelectProps extends React.SelectHTMLAttributes<HTMLSelectElement> {
id: string
// ...other props
}
interface DotsLoaderProps extends React.HTMLAttributes<HTMLSpanElement> {
color?: string
// ...other props
}
interface IconProps extends React.SVGProps<SVGSVGElement> {
color?: string
width?: number | string
height?: number | string
// ...other props
}
// this works for all headings (H1, H2, ...) 👇
interface HeadingProps extends React.HTMLAttributes<HTMLHeadingElement> {
// ...other props
}
interface HeadingProps extends React.HTMLAttributes<HTMLLIElement> {
// ...other props
}
// How to creat Types for forms
interface CompanyName extends HTMLFormControlsCollection {
companyName: HTMLInputElement
}
interface CompanyNameFormElement extends HTMLFormElement {
readonly elements: CompanyName
}
function handleSubmit(event: React.FormEvent<CompanyNameFormElement>) {
event.preventDefault()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment