-
-
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
Merged
timdorr
merged 1 commit into
reduxjs:ts-conversion
from
cellog:cellog-3500-actions-merge
Aug 30, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* An *action* is a plain object that represents an intention to change the | ||
* state. Actions are the only way to get data into the store. Any data, | ||
* whether from UI events, network callbacks, or other sources such as | ||
* WebSockets needs to eventually be dispatched as actions. | ||
* | ||
* Actions must have a `type` field that indicates the type of action being | ||
* performed. Types can be defined as constants and imported from another | ||
* module. It's better to use strings for `type` than Symbols because strings | ||
* are serializable. | ||
* | ||
* Other than `type`, the structure of an action object is really up to you. | ||
* If you're interested, check out Flux Standard Action for recommendations on | ||
* how actions should be constructed. | ||
* | ||
* @template T the type of the action's `type` tag. | ||
*/ | ||
export interface Action<T = any> { | ||
type: T | ||
} | ||
|
||
/** | ||
* An Action type which accepts any other properties. | ||
* This is mainly for the use of the `Reducer` type. | ||
* This is not part of `Action` itself to prevent types that extend `Action` from | ||
* having an index signature. | ||
*/ | ||
export interface AnyAction extends Action { | ||
// Allows any extra properties to be defined in an action. | ||
[extraProps: string]: any | ||
} | ||
|
||
/* action creators */ | ||
|
||
/** | ||
* An *action creator* is, quite simply, a function that creates an action. Do | ||
* not confuse the two terms—again, an action is a payload of information, and | ||
* an action creator is a factory that creates an action. | ||
* | ||
* Calling an action creator only produces an action, but does not dispatch | ||
* it. You need to call the store's `dispatch` function to actually cause the | ||
* mutation. Sometimes we say *bound action creators* to mean functions that | ||
* call an action creator and immediately dispatch its result to a specific | ||
* store instance. | ||
* | ||
* If an action creator needs to read the current state, perform an API call, | ||
* or cause a side effect, like a routing transition, it should return an | ||
* async action instead of an action. | ||
* | ||
* @template A Returned action type. | ||
*/ | ||
export interface ActionCreator<A> { | ||
(...args: any[]): A | ||
} | ||
|
||
/** | ||
* Object whose values are action creator functions. | ||
*/ | ||
export interface ActionCreatorsMapObject<A = any> { | ||
[key: string]: ActionCreator<A> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 forActionCreator
, so we can be 'type-safer' when calling those? i.e:setCategory('foo')
andsetCategory(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 toany
in case it's not declared? CheersThere 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:Whereas it wouldn't happen for any as it can be assigned to
string
in this case without a problem. So I guess, assigning tounknown
, 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
is not good practice.
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.