Skip to content
This repository has been archived by the owner on Sep 16, 2020. It is now read-only.

Commit

Permalink
refactor: amend the reducer type
Browse files Browse the repository at this point in the history
  • Loading branch information
Finesse committed Oct 15, 2019
1 parent daacb43 commit bce8d1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 5 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export type Symbiotes<State> = {
[Key in any]: Symbiote<State, any[]> | Symbiotes<State>;
};

export type Reducer<State> = (state: State, action: Action<any[]>) => State;
interface BasicAction {
type: string | number | symbol;
}

export type Reducer<State> = (state: State, action: BasicAction) => State;

export type ActionCreator<TSymbiote> = TSymbiote extends Symbiote<any, infer Arguments>
? (...payload: Arguments) => Action<Arguments>
Expand Down
6 changes: 4 additions & 2 deletions types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ interface PlainActionCreators {
{
// Works correct with plain state and actions

const initialState = { count: 0 };
const { actions, reducer } = createSymbiote(
{ count: 0 },
initialState,
{
inc: (state: PlainState) => ({ ...state, count: state.count + 1 }),
dec: (state: PlainState) => ({ ...state, count: state.count + 1 }),
Expand All @@ -37,7 +38,8 @@ interface PlainActionCreators {
reducer as Reducer<PlainState>;

actions.inc();
actions.dec();
reducer(initialState, actions.dec());
reducer(initialState, { type: 'other' });
}
{
// Throw error if the initial state type doesn't match the symbiotes state type
Expand Down

0 comments on commit bce8d1d

Please sign in to comment.