Skip to content

Commit

Permalink
Refine typing of base reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Jul 27, 2022
1 parent 633709a commit 9aef4eb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/components/src/input-control/reducer/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface EventPayload {
event?: SyntheticEvent;
}

interface Action< Type, ExtraPayload = {} > {
export interface Action< Type = string, ExtraPayload = {} > {
type: Type;
payload: EventPayload & ExtraPayload;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/input-control/reducer/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function mergeInitialState(
*/
function inputControlStateReducer(
composedStateReducers: StateReducer
): StateReducer {
): StateReducer< actions.ControlAction > {
return ( state, action ) => {
const nextState = { ...state };

Expand Down Expand Up @@ -151,7 +151,7 @@ export function useInputControlStateReducer(
initialState: Partial< InputState > = initialInputControlState,
onChangeHandler: InputChangeCallback
) {
const [ state, dispatch ] = useReducer< StateReducer >(
const [ state, dispatch ] = useReducer(
inputControlStateReducer( stateReducer ),
mergeInitialState( initialState )
);
Expand Down
7 changes: 5 additions & 2 deletions packages/components/src/input-control/reducer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Reducer, SyntheticEvent } from 'react';
/**
* Internal dependencies
*/
import type { ControlAction, InputAction } from './actions';
import type { Action, InputAction } from './actions';

export interface InputState {
_event?: SyntheticEvent;
Expand All @@ -19,7 +19,10 @@ export interface InputState {
value?: string;
}

export type StateReducer = Reducer< InputState, InputAction | ControlAction >;
export type StateReducer< SpecializedAction = Action > = Reducer<
InputState,
InputAction | SpecializedAction
>;

export const initialStateReducer: StateReducer = ( state: InputState ) => state;

Expand Down

0 comments on commit 9aef4eb

Please sign in to comment.