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

Infer action types from action creators instead of using separate Flow types #11912

Closed
kumar303 opened this issue Jun 19, 2018 · 6 comments
Closed
Labels
repository:addons-frontend Issue relating to addons-frontend state:stale Issues marked as stale. These can be re-opened should there be plans to fix them.

Comments

@kumar303
Copy link
Contributor

This article has a recipe for inferring action types from action creator functions instead of defining, exporting, and maintaining separate action types. This will make everything simpler.

For example, instead of this:

export const SET_NAME = 'SET_NAME';
export const SET_AGE = 'SET_AGE';

export type SetName = {type: 'SET_NAME', payload: string}
export type SetAge = {type: 'SET_AGE', payload: number}

const setName = (name: string): SetName => {
    return {type: SET_NAME, payload: name}
}

const setAge = (age: number): SetAge => {
    return {type: SET_NAME, payload: age}
}

export type Actions = SetName | SetAge;

We can do this:

export const SET_NAME = 'SET_NAME';
export const SET_AGE = 'SET_AGE';

const setName = (name: string) => {
    return {type: SET_NAME, payload: name}
}

const setAge = (age: number) => {
    return {type: SET_AGE, payload: age}
}

export type Actions =
    ExtractReturn<typeof setName> |
    ExtractReturn<typeof setAge>
@kumar303
Copy link
Contributor Author

There is a better version here: facebook/flow#4002 (comment)

@kumar303
Copy link
Contributor Author

kumar303 commented Jun 19, 2018

Well, huh. I can get things to work in Flow Try but not in our code. It seems to always set action and all of its properties to any which is not helpful. That is, it's not actually extracting the type of the returned object.

@bobsilverberg
Copy link
Contributor

I just tried doing this as well, for a new action I was adding to the collections reducer, and noticed that flow reports the type as any | {|payload: {|username: string|}, type: "UNLOAD_USER_COLLECTIONS"|}, so the any is breaking Flow's ability to type check. I'm also not sure why it works on Flow Try but not in our code.

@bobsilverberg
Copy link
Contributor

Actually, I take that back. I think it does work. I'll put up a PR and someone else can check to see if it looks like it's working.

@bobsilverberg
Copy link
Contributor

Ugh, now I cannot get it to work again. :-( Nevermind me.

@stale
Copy link

stale bot commented Sep 18, 2019

This issue has been automatically marked as stale because it has not had recent activity. If you think this bug should stay open, please comment on the issue with further details. Thank you for your contributions.

@stale stale bot added the state:stale Issues marked as stale. These can be re-opened should there be plans to fix them. label Sep 18, 2019
@stale stale bot closed this as completed Oct 2, 2019
@KevinMind KevinMind transferred this issue from mozilla/addons-frontend May 5, 2024
@KevinMind KevinMind added repository:addons-frontend Issue relating to addons-frontend migration:2024 labels May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
repository:addons-frontend Issue relating to addons-frontend state:stale Issues marked as stale. These can be re-opened should there be plans to fix them.
Projects
None yet
Development

No branches or pull requests

5 participants