Skip to content
New issue

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

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

Already on GitHub? # to your account

Incorrect type inference of the action callback arguments #2211

Closed
7 tasks done
kubk opened this issue Nov 18, 2019 · 4 comments · Fixed by #2213
Closed
7 tasks done

Incorrect type inference of the action callback arguments #2211

kubk opened this issue Nov 18, 2019 · 4 comments · Fixed by #2213

Comments

@kubk
Copy link
Collaborator

kubk commented Nov 18, 2019

I have an:
[x] Issue:

  • Provide a minimal sample reproduction: https://codesandbox.io/s/react-mobx-starter-t8xju
    Go to components/App.tsx
  • Did you check this issue wasn't filed before? It looks like there was a fix in 2016, which solved the same issue: Typescript: Add granular overloads for action, which fixes "Parameter XXX implicitly has an 'any' type" #500
  • Elaborate on your issue. What behavior did you expect?
    I am expecting correct type inference for the arguments of action callback. All arguments have type 'unknown':
    image
    Expected type 'number'.
  • State the versions of MobX and relevant libraries. Which browser / node / ... version? Mobx 5.9.4, TypeScript 3.4.5
  • What problem would it solve for you? It would allow to use type inference instead of manually specifying each argument type of the action callback.
  • Do you think others will benefit from this change as well and it should in core package (see also mobx-utils)? Yes, because it looks like type inference worked with previous Mobx/TypeScript versions.
  • Are you willing to (attempt) a PR yourself? I already tried to fix the issue but with no success.
@danielkcz
Copy link
Contributor

danielkcz commented Nov 19, 2019

Um, since you obviously have CodeSandbox in that picture, can you please share the link as well? The one you linked above does not have such code in App.tsx.

@kubk
Copy link
Collaborator Author

kubk commented Nov 19, 2019

@FredyC Sorry, I updated the link and removed unrelated code.

@kubk kubk changed the title Incorrect type inference of the the action callback arguments Incorrect type inference of the action callback arguments Nov 19, 2019
@danielkcz
Copy link
Contributor

Thanks. Well, I am not so sure action can infer that argument properly. And if it would be possible, it would be a lot of hassle for a little gain. Have a look at the current typings for actions.

<A1, A2, A3, A4, A5, A6, R, T extends (a1: A1, a2: A2, a3: A3, a4: A4, a6: A6) => R>(fn: T): T &

It's already crazy overload hell. Expanding that to get the correct argument type might not be worth it. I think this is one of the cases where explicit type casting will be necessary ... action((arg: number) => ....

I will leave this open if someone else wants to chime.

@lock
Copy link

lock bot commented Jan 24, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 24, 2020
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants