react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

unable to use ref with useCheckbox hook with Typescript... types of property 'capture' are incompatible.

Open bstro opened this issue 5 years ago • 6 comments

🐛 Bug Report

This seems like a bug based on a simple use of useCheckbox. Here's the code:

let ref = React.useRef<HTMLInputElement>(null)
let {inputProps} = useCheckbox(props, state, ref)
return <input {...inputProps} ref={ref} />

🤔 Expected Behavior

This should compile without type errors on the latest major version of Typescript (4.4).

😯 Current Behavior

I receive the following type error:

Type '{ ref: RefObject<HTMLInputElement>; accept?: string | undefined; alt?: string | undefined; autoComplete?: string | undefined; autoFocus?: boolean | undefined; ... 281 more ...; onTransitionEndCapture?: TransitionEventHandler<...> | undefined; }' is not assignable to type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.
  Type '{ ref: RefObject<HTMLInputElement>; accept?: string | undefined; alt?: string | undefined; autoComplete?: string | undefined; autoFocus?: boolean | undefined; ... 281 more ...; onTransitionEndCapture?: TransitionEventHandler<...> | undefined; }' is not assignable to type 'InputHTMLAttributes<HTMLInputElement>'.
    Types of property 'capture' are incompatible.
      Type 'string | boolean | undefined' is not assignable to type 'boolean | "user" | "environment" | undefined'.
        Type 'string' is not assignable to type 'boolean | "user" | "environment" | undefined'.ts(2322)
-------------------------------------------------------------------------------------------------------------
(property) JSX.IntrinsicElements.input: React.DetailedHTMLProps<React.InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>

This error disappears if I remove the inputProps from the component.

💁 Possible Solution

Unclear

🔦 Context

Here's a codesandbox with my exact code. Curiously, the type error does not appear in this codesandbox. I have noticed that codesandbox seems to use typescript 4.1.2 and it doesn't seem to be possible to change that currently. Locally, I am using typescript 4.4.3.

https://codesandbox.io/s/nice-sinoussi-g5sh9?file=/src/App.tsx:272-300

🌍 Your Environment

Software Version(s)
react-aria 3.10.0
react-stately 3.9.0
typescript 4.4.3

bstro avatar Oct 27 '21 17:10 bstro

Not sure what we can do on our end since those types are from @types/react, guess there is a mismatch here between whats on latest and the version we have?

LFDanLu avatar Nov 03 '21 19:11 LFDanLu

Unclear to me. I've upgraded the @types packages for react and react-dom, both in my project and in the codesandbox to the latest available, and the error doesn't appear in the codesandbox, but it does appear in my local project.

I tried changing the version of typescript in my local project to 4.2.3 to match the version of typescript hosted by codesandbox, but the error persists. I'm having trouble isolating the issue. I'll keep poking around.

fwiw this looks like the relevant line from @types/react

bstro avatar Nov 03 '21 23:11 bstro

do you have multiple copies of @types/react installed? might cause a conflict

snowystinger avatar Nov 03 '21 23:11 snowystinger

Currently experiencing the same issue. I don't have multiple copies of @types/react installed. The issue occurs when upgrading @react/types from 17.0.22 -> 17.0.38

aidan-keay avatar Jan 21 '22 15:01 aidan-keay

Is this still an issue? Can anyone provide a minimal github project with the issue since it doesn't appear to happen in codesandbox

snowystinger avatar Mar 03 '25 05:03 snowystinger

closing as stale

snowystinger avatar May 29 '25 02:05 snowystinger