Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@types/react] button refuses its own props #36505

Open
4 tasks done
brkc opened this issue Jun 27, 2019 · 13 comments
Open
4 tasks done

[@types/react] button refuses its own props #36505

brkc opened this issue Jun 27, 2019 · 13 comments

Comments

@brkc
Copy link

brkc commented Jun 27, 2019

const Button = (props: React.HTMLProps<HTMLButtonElement>) => (
  <button {...props} />
);
error TS2322: Type '{ accept?: string | undefined; acceptCharset?: string | undefined; action?: string | undefined; allowFullScreen?: boolean | undefined; allowTransparency?: boolean | undefined; alt?: string | undefined; ... 353 more ...; key?: string | ... 1 more ... | undefined; }' is not assignable to type 'DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>'.
  Type '{ accept?: string | undefined; acceptCharset?: string | undefined; action?: string | undefined; allowFullScreen?: boolean | undefined; allowTransparency?: boolean | undefined; alt?: string | undefined; ... 353 more ...; key?: string | ... 1 more ... | undefined; }' is not assignable to type 'ButtonHTMLAttributes<HTMLButtonElement>'.
    Types of property 'type' are incompatible.
      Type 'string | undefined' is not assignable to type '"button" | "reset" | "submit" | undefined'.
        Type 'string' is not assignable to type '"button" | "reset" | "submit" | undefined'.

   <button {...props} />
    ~~~~~~


Found 1 error.
@jakejrichards
Copy link
Contributor

I think what you're probably looking for is React.HTMLAttributes<HTMLButtonElement>.

@ferdaber
Copy link
Contributor

ferdaber commented Jul 3, 2019

Jake has it right. It might even be easier to do JSX.IntrinsicElements['button'] for easier memorization.

@praxxis
Copy link
Contributor

praxxis commented Aug 14, 2019

I had the most success using React.ComponentProps<'button'> - using HTMLAttributes meant the disabled prop was missing for some reason

@martpie
Copy link
Contributor

martpie commented Nov 4, 2019

So is React.ComponentProps<'button'> a workaround (or the official solution)? or is React.HTMLProps<HTMLButtonElement> "broken"?

@ferdaber
Copy link
Contributor

ferdaber commented Nov 4, 2019

HTMLProps is the common set all attributes that every HTML element can possibly have, so actually there's another interface called ButtonHTMLAttributes that have button-specific props. In the specific case of this issue, ButtonHTMLAttributes narrows the type of the type attribute even further to just an enumeration of those string literals ('submit' | 'button' rather than just the general string type).

So React.HTMLProps<HTMLButtonElement> is not sufficient to describe the set of attributes that an HTML button element can have. The proper (and easiest) solution is to just use ComponentProps<'button'> which automatically seeks the most appropriate interface you need for a host element.

@ElForastero
Copy link

In my case, type of the props was almost likely incorrect after partially spreading these props.

export const Button: Overload = ({
  color = 'primary',
  className,
  href,
  ...rest
}: ButtonProps | AnchorProps) => {
    // here, in ...rest, "type" property of the button will be "string | undefined".
}

So I had to force the rest-spreaded props like this:

<button className={clx} style={style} {...rest as ButtonProps}>
    {children}
</button>

@FDiskas
Copy link

FDiskas commented Mar 8, 2021

Works for me:

React.ButtonHTMLAttributes<HTMLButtonElement>

@DominicTobias-b1
Copy link

In my case, type of the props was almost likely incorrect after partially spreading these props.

Incorrect seems like a stretch since you're saying it could also be AnchorProps and anchors don't have a type attribute

@ltruchot
Copy link

As another workaround:
interface ButtonProps extends Omit<HTMLProps<HTMLButtonElement>, 'type'> { //...

@StanleySathler
Copy link

If you're using React.ComponentProps<'button'> and see a weird error about incompatible ref types, try using React.ComponentPropsWithRef<'button'> or React.ComponentPropsWithoutRef<'button'> instead.

@juanmsl
Copy link

juanmsl commented Nov 16, 2021

I found this issue for the same reason and this work for me

interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> { ... }

@ghost
Copy link

ghost commented Mar 22, 2022

React.HTMLAttributes

Thank you!

@Weswwn
Copy link

Weswwn commented Jul 28, 2022

Is there a preferred type method?

React.ComponentProps<'button'> vs React.ButtonHTMLAttributes<HTMLButtonElement>

It seems from the comments above that the React.ComponentProps<'button'> is causing some ref issues which makes me lean more towards the latter way of typing our Button elements.

ocboogie added a commit to eed-web-application/elog-plus-frontend that referenced this issue Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests