I have a component defined as follows:
function ConfirmableButton(props: { onConfirm: () => void; } & HTMLAttributes<HTMLButtonElement>)
But when using it like
<ConfirmableButton disabled={disabledRows.includes(id)} />
, it gives me error Property 'disabled' does not exist on type 'IntrinsicAttributes & { onConfirm: () => void; } & HTMLAttributes<HTMLButtonElement>'
What should I be using to make sure the typing is correct?
2
Answers
The generic type on
HTMLAttributes<T>
is only really there to give the specific element type to its event handlers. Checking the types,T
inHTMLAttributes
is only referenced where it extendsDOMAttributes<T>
, which is passed to each event handler to type the target property of the event object.To get
<button>
-specific attributes, you would need to use React’sButtonHTMLAttributes<HTMLButtonElement>
(which is the type React would use for a button element):You need to define the
props
forConfirmableButton
as follows:This will let you use the component like this: