Skip to content

Instantly share code, notes, and snippets.

@treyhuffine
Last active March 5, 2019 00:55
Show Gist options
  • Save treyhuffine/4712d1ea004dafa8e3c534fcd6078de2 to your computer and use it in GitHub Desktop.
Save treyhuffine/4712d1ea004dafa8e3c534fcd6078de2 to your computer and use it in GitHub Desktop.
type Dispatch<A> = (value: A) => void;
type Reducer<S, A> = (prevState: S, action: A) => S;
type ReducerState<R extends Reducer<any, any>> = R extends Reducer<infer S, any> ? S : never;
type ReducerAction<R extends Reducer<any, any>> = R extends Reducer<any, infer A> ? A : never;
function useReducer<R extends Reducer<any, any>, I>(
reducer: R,
initializerArg: I & ReducerState<R>,
initializer: (arg: I & ReducerState<R>) => ReducerState<R>
): [ReducerState<R>, Dispatch<ReducerAction<R>>];
function useReducer<R extends Reducer<any, any>, I>(
reducer: R,
initializerArg: I,
initializer: (arg: I) => ReducerState<R>
): [ReducerState<R>, Dispatch<ReducerAction<R>>];
function useReducer<R extends Reducer<any, any>>(
reducer: R,
initialState: ReducerState<R>,
initializer?: undefined
): [ReducerState<R>, Dispatch<ReducerAction<R>>];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment