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

Type errors when using typescript in v6.0.0 #738

Closed
bggolding opened this issue Oct 5, 2022 · 5 comments
Closed

Type errors when using typescript in v6.0.0 #738

bggolding opened this issue Oct 5, 2022 · 5 comments
Labels

Comments

@bggolding
Copy link

Version

6.0.0

Description

When I updated my typescript project to use v6.0.0, there are errors appearing because the generic type of the Option that the AsyncTypeahead and Typeahead components use has been removed. When I remove the generic argument in my code, I get errors in my code because the types of Typeahead’s Option type don’t match my object.

I think the use of the generic type on AsyncTypeahead and Typeahead that was in the DefinitelyTyped types for the v5 version was exactly the right way to do this and it would be great if that was reinstated. This would also ease the upgrade path for typescript projects.

I have put together a codesandbox example that shows the error:

https://codesandbox.io/s/pensive-antonelli-o84wbj?file=/src/index.tsx

In my project, I’m getting errors on the labelKey prop as well. I’m not sure why that’s not showing in my example.

Expected Behavior

It shouldn’t be necessary to bypass the typing system on the onChange prop to get the code to build cleanly.

Actual Behavior

I get this error:

Type 'Dispatch<SetStateAction<StateStat[]>>' is not assignable to type '(selected: Option[]) => void'.
  Types of parameters 'value' and 'selected' are incompatible.
    Type 'Option[]' is not assignable to type 'SetStateAction<StateStat[]>'.
      Type 'Option[]' is not assignable to type 'StateStat[]'.
        Type 'Option' is not assignable to type 'StateStat'.
          Type 'string' is not assignable to type 'StateStat'.ts(2322)
@bggolding bggolding added the bug label Oct 5, 2022
@ericgio
Copy link
Owner

ericgio commented Oct 6, 2022

I believe this is a duplicate of #704.

You should be able to type the argument of onChange to get around this:

<Typeahead
  ...
  onChange={(selected: StateStat[]) => setSelected(selected)}
/>

@ericgio ericgio closed this as completed Oct 6, 2022
@bggolding
Copy link
Author

This doesn’t really feel like a very typescript-y solution: you shouldn’t need to re-type the argument. It will make it more painful for typescript users to upgrade to v6.

@ericgio
Copy link
Owner

ericgio commented Oct 6, 2022

Yeah as I alluded, it's a bit of a workaround. There's a bit more detail in the original issue, but truly fixing this problem with generics is pretty involved and has its own tradeoffs.

@bggolding
Copy link
Author

Yeah, I understand it’s a work around and I can appreciate that there’s a bit of work to really fix the problem.

Is it worth contacting the people who did the DefinitelyTyped definitions? They did a really great job there

@ericgio
Copy link
Owner

ericgio commented Oct 11, 2022

Is it worth contacting the people who did the DefinitelyTyped definitions? They did a really great job there

I've taken a look at the DT types and while they did do a nice job, one of the key differences is that they're only declaring types for the public API. To truly type this library in a comprehensive way, I'd also need to propagate types (specifically option generics) throughout the private API, which is significant.

I'm definitely open to ideas for how to provide type flexibility without significantly impacting the maintainability of the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants