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

extract action types from index.d.ts #3541

Merged
merged 1 commit into from
Aug 30, 2019

Conversation

cellog
Copy link
Contributor

@cellog cellog commented Aug 30, 2019

Extract all things action types from index.d.ts

Note: this PR also fixes an oversight in the extraction of store-related types, which is the declaration of observable in types/store.ts

@timdorr timdorr merged commit 7bd5846 into reduxjs:ts-conversion Aug 30, 2019
@cellog cellog deleted the cellog-3500-actions-merge branch August 30, 2019 15:58
* @template A Returned action type.
*/
export interface ActionCreator<A> {
(...args: any[]): A
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, @cellog
Woudn't it be a good idea allowing a declaration for the args type as second generic for ActionCreator, so we can be 'type-safer' when calling those? i.e: setCategory('foo') and setCategory(false) would be both valid? It may not cover all cases, but at least it would be a bit safer and always have a fallback to any in case it's not declared? Cheers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good catch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your PR I recommend you default to unknown[] first instead of any[] and see if that works out

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! There you go:
cellog#3
Perhaps I picked the wrong branch to merge. Please let me know if I should change it to reduxjs/redux instead :)

The only problem with defaulting to unknown is:

import type {Action,ActionCreator} from 'redux';

type FooAction  = Action<'SET_FOO'> & {value:string};
type FooActionCreator = ActionCreator<FooAction>;

const setFoo:FooActionCreator = value => ({type:'SET_FOO', >>value<<});
// Type 'unknown' is not assignable to type 'string'.

Whereas it wouldn't happen for any as it can be assigned to string in this case without a problem. So I guess, assigning to unknown, could make it a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, definitely switch it to reduxjs/redux unless you want it to be inaccessible to the future :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it would be a breaking change in that it would force downstream types to be declared. Thus the above would have to be ActionCreator<FooAction, [string]>;

Keep in mind that

const setFoo:FooActionCreator = value => ({type:'SET_FOO', value });

is not good practice.

const setFoo = (value: string): FooAction => ({type:'SET_FOO', value });

is the best way to declare an action creator. ActionCreator should only be used in functions that want to accept an action creator as a parameter, or in other settings that consume them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's, great. Thanks for the suggestion. I will certainly keep that in mind as it makes sense.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zanona that suggestion for the extra type param was very neat. Makes redux-toolkit/typescript work like magic.

webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
webMasterMrBin pushed a commit to webMasterMrBin/redux that referenced this pull request Aug 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants