-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
extract action types from index.d.ts #3541
Conversation
* @template A Returned action type. | ||
*/ | ||
export interface ActionCreator<A> { | ||
(...args: any[]): A |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good catch
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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