diff --git a/types/use-global-hook/index.d.ts b/types/use-global-hook/index.d.ts index 19ad7e56a8..69c762c071 100644 --- a/types/use-global-hook/index.d.ts +++ b/types/use-global-hook/index.d.ts @@ -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 { - state: S; - actions: A; - setState(state: S, afterUpdateCallback?: () => void): void; + state: S; + actions: A; + setState(state: S, afterUpdateCallback?: () => void): void; } export type InitializerFunction = (store: Store) => void; +type UseGlobal = (() => [S, A]) & + ((stateFunc: (state: S) => NS) => [NS, A]) & + ((stateFunc: (state: S) => NS, actionsFunc: (state: A) => NA) => [NS, NA]) & + ((stateFunc: undefined, actionsFunc: (state: A) => NA) => [S, NA]); + export default function useStore( - React: ReactType, - inititalState: S, - actions: object, - initializers?: InitializerFunction, -): (stateFunc?: (state: S) => NS, actionsFunc?: (state: A) => NA) => [NS, NA]; + React: ReactInterface, + inititalState: S, + actions: object, + initializers?: InitializerFunction, +): UseGlobal; diff --git a/types/use-global-hook/use-global-hook-tests.ts b/types/use-global-hook/use-global-hook-tests.ts index c2ceaaa05e..3313fef7f9 100644 --- a/types/use-global-hook/use-global-hook-tests.ts +++ b/types/use-global-hook/use-global-hook-tests.ts @@ -17,12 +17,24 @@ const initializer: InitializerFunction = (stor store.setState({ value: 'string' }); }; +// with initializer const store = useStore(React, { value: '' }, {}, initializer); -store(); // $ExpectType [unknown, unknown] -store(); // $ExpectType [stateType, associatedActionsType] -store((state: stateType) => state.value); // $ExpectType [string, associatedActionsType] -store(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((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(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((state: stateType) => state.value, (actions: associatedActionsType) => actions.setValue); // $ExpectType [string, setFunc] +// without initializer useStore(React, { value: '' }, {});