[use-global-hook] Updates def for simpler usage (#39199)

* [use-global-hook] Updates def for simpler usage

* [use-global-hook] make recommended changes

* [use-global-hook] remove lint suppression
This commit is contained in:
James Hong 2019-10-23 08:07:36 -07:00 committed by Wesley Wigham
parent 4700dcf113
commit edb4917e1b
2 changed files with 35 additions and 14 deletions

View File

@ -4,23 +4,32 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.6
import React = require('react');
type ReactType = typeof React;
// Use an interface so that different versions of React can be used
interface ReactInterface {
useEffect: (...args: any[]) => any;
useState: (...args: any[]) => any;
useMemo: (...args: any[]) => any;
}
// to ignore strict-export-declare-modifiers error
export {};
// Where S is typeof state and A is typeof associated actions
export interface Store<S, A> {
state: S;
actions: A;
setState(state: S, afterUpdateCallback?: () => void): void;
state: S;
actions: A;
setState(state: S, afterUpdateCallback?: () => void): void;
}
export type InitializerFunction<S, A> = (store: Store<S, A>) => void;
type UseGlobal<S, A> = (() => [S, A]) &
(<NS>(stateFunc: (state: S) => NS) => [NS, A]) &
(<NS, NA>(stateFunc: (state: S) => NS, actionsFunc: (state: A) => NA) => [NS, NA]) &
(<NA>(stateFunc: undefined, actionsFunc: (state: A) => NA) => [S, NA]);
export default function useStore<S, A>(
React: ReactType,
inititalState: S,
actions: object,
initializers?: InitializerFunction<S, A>,
): <NS, NA>(stateFunc?: (state: S) => NS, actionsFunc?: (state: A) => NA) => [NS, NA];
React: ReactInterface,
inititalState: S,
actions: object,
initializers?: InitializerFunction<S, A>,
): UseGlobal<S, A>;

View File

@ -17,12 +17,24 @@ const initializer: InitializerFunction<stateType, associatedActionsType> = (stor
store.setState({ value: 'string' });
};
// with initializer
const store = useStore<stateType, associatedActionsType>(React, { value: '' }, {}, initializer);
store(); // $ExpectType [unknown, unknown]
store<stateType, associatedActionsType>(); // $ExpectType [stateType, associatedActionsType]
store<string, associatedActionsType>((state: stateType) => state.value); // $ExpectType [string, associatedActionsType]
store<stateType, setFunc>(undefined, (action: associatedActionsType) => action.setValue); // $ExpectType [stateType, setFunc]
// default
store(); // $ExpectType [stateType, associatedActionsType]
// works without passing expected type when using state filter
store((state: stateType) => state.value); // $ExpectType [string, associatedActionsType]
store<string>((state: stateType) => state.value); // $ExpectType [string, associatedActionsType]
// works without passing expected type when using only action filter
store(undefined, (action: associatedActionsType) => action.setValue); // $ExpectType [stateType, setFunc]
// returns expected type if passed types
store<setFunc>(undefined, (action: associatedActionsType) => action.setValue); // $ExpectType [stateType, setFunc]
// works without passing expected type when using both state and action filters
store((state: stateType) => state.value, (actions: associatedActionsType) => actions.setValue); // $ExpectType [string, setFunc]
store<string, setFunc>((state: stateType) => state.value, (actions: associatedActionsType) => actions.setValue); // $ExpectType [string, setFunc]
// without initializer
useStore<stateType, associatedActionsType>(React, { value: '' }, {});