Created
July 24, 2019 18:16
-
-
Save chrisdhanaraj/001f9ae3c775a0d25121024ec990175a to your computer and use it in GitHub Desktop.
Typings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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