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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/types/actions.ts
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
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.

}

/**
* Object whose values are action creator functions.
*/
export interface ActionCreatorsMapObject<A = any> {
[key: string]: ActionCreator<A>
}
4 changes: 3 additions & 1 deletion src/types/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Action, Reducer, AnyAction, Dispatch, PreloadedState } from '../..'
/// <reference types="symbol-observable" />
import { Reducer, Dispatch, PreloadedState } from '../..'
import { Action, AnyAction } from './actions'

/**
* Function to remove listener added by `Store.subscribe()`.
Expand Down