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

[bug]: EventHandler type should allow boolean types #2324

Open
1 of 5 tasks
richardaum opened this issue Sep 26, 2024 · 0 comments
Open
1 of 5 tasks

[bug]: EventHandler type should allow boolean types #2324

richardaum opened this issue Sep 26, 2024 · 0 comments
Labels
template: bug This issue might be a bug

Comments

@richardaum
Copy link

richardaum commented Sep 26, 2024

Which react-spring target are you using?

  • @react-spring/web
  • @react-spring/three
  • @react-spring/native
  • @react-spring/konva
  • @react-spring/zdog

What version of react-spring are you using?

9.7.4

What's Wrong?

If we use any callback lets say for useTransition hook, type inference will fail if you use boolean as data. While investigating the root cause, I stumble upon this EventHandler. By reducing it to a single parameter function definition I could identify the issue with boolean type.

type EventHandler<Item = undefined> = 
  Item extends undefined ? 
    (item?: Item) => void : 
    (item: Item) => void;

To Reproduce

If you try to assign EventHandler to an arrow function to see the error happening, you'll notice the issue, as pointed in here, but also below:

type EventHandler<Item = undefined> = Item extends undefined ? (item?: Item) => void : (item: Item) => void;

const failingEventHandlerInference: EventHandler<boolean> = (item) => { } // fails
const goodEventHandlerInference: EventHandler<string> = (item) => { } // succeeds

Expected Behaviour

The real issue is that Item extends undefined that simply doesn't work with boolean type. Instead, as I expected both source code and who uses EventHandlers doesn't simply omit the item argument but provides a undefined-ish value, we could do:

type EventHandler<Item = undefined> = 
  (item: Item) => void

That will still allow Item to be inferred properly. After locally testing it by applying that change directly on my node_modules/@react-spring/types/dist/react-spring_types.modern.d.ts, it worked. As I simplified the EventHandler the actual change would be:

type EventHandler<TResult extends Readable = any, TSource = unknown, Item = undefined> = (result: AnimationResult<TResult>, ctrl: TSource, item: Item | undefined) => void;

Source code

Link to repo

@richardaum richardaum added the template: bug This issue might be a bug label Sep 26, 2024
@richardaum richardaum changed the title [bug]: EventHandler should prevent boolean types [bug]: EventHandler type should allow boolean types Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
template: bug This issue might be a bug
Projects
None yet
Development

No branches or pull requests

1 participant