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

Issue with generic types on action #1829

Closed
9 tasks done
tv1ster opened this issue Dec 5, 2018 · 3 comments
Closed
9 tasks done

Issue with generic types on action #1829

tv1ster opened this issue Dec 5, 2018 · 3 comments

Comments

@tv1ster
Copy link

tv1ster commented Dec 5, 2018

I have a:

  1. Issue:
  • Provide error messages including stacktrace

TypeScript error Type '{}' is not assignable to type 'number'.

  • Provide a minimal sample reproduction. Create a reproduction based on this sandbox

Seems like generics for action not really working well. In real project I am triggering some simple action on rxjs subscription. But I created small example to show you the issue. I tried to duplicate your code for IActionFactory so it will be easier to understand the rootcause.

  • Did you check this issue wasn't filed before?
  • Elaborate on your issue. What behavior did you expect?

I expect TS to not throw error, because it have already all info about my types.

  1. Idea:
  • What problem would it solve for you?

For real, problem is not so huge, I just need to define some types for action, and TS will understand what is going on. But it would be great, if everything would work without strict types declaration.

  • Do you think others will benefit from this change as well and it should in core package (see also mobx-utils)?
  • Are you willing to (attempt) a PR yourself?

I found out, that issue will dissappear, if I will move generic types at top, like here, but I'm not sure about full impact of this change, so decided ask you about this issue. Maybe I'm doing something wrong? Because I'm not fully understand, why do we need these types

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

while we can use only

<T extends Function>(fn: T): T & IAction
@tv1ster
Copy link
Author

tv1ster commented Dec 5, 2018

UPD. Also, seems like all this stuff

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

can be refactored to

  <A extends any[], R, T extends (...A) => R>(fn: T): T & IAction

UPD.UPD. Seems like you have already discussed it here

@mweststrate
Copy link
Member

Closing as duplicate

@lock
Copy link

lock bot commented Jul 21, 2019

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 Jul 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants