Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdhanaraj/001f9ae3c775a0d25121024ec990175a to your computer and use it in GitHub Desktop.
Save chrisdhanaraj/001f9ae3c775a0d25121024ec990175a to your computer and use it in GitHub Desktop.
Typings
// Type definitions for use-reducer-with-side-effects 0.4
// Project: https://github.com/baz/foo (Does not have to be to GitHub, but prefer linking to a source code repository rather than to a project website.)
// Definitions by: Chris Dhanaraj <https://github.com/chrisdhanaraj>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/*~ If this module has methods, declare them as functions like so.
*/
type SideEffectArguments = {
state: any;
dispatch: (action: object) => void;
};
type SideEffectReturn = void | (() => void);
type SideEffectCallback = (state: any, dispatch: SideEffectArguments) => 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;
type Dispatch<A> = (value: A) => void;
export function UpdateWithSideEffect(newState: any, newSideEffect: SideEffectCallback): SideEffectReturn;
export function NoUpdate(): Symbol;
export function SideEffect(newSideEffect: SideEffectCallback): void;
export function useReducerWithSideEffects<R extends Reducer<any, any>, I>(
reducer: R,
initializerArg: I & ReducerState<R>,
initializer: (arg: I & ReducerState<R>) => ReducerState<R>
): [ReducerState<R>, Dispatch<ReducerAction<R>>];
export function useReducer<R extends Reducer<any, any>, I>(
reducer: R,
initializerArg: I,
initializer: (arg: I) => ReducerState<R>
): [ReducerState<R>, Dispatch<ReducerAction<R>>];
export 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