Adds type support for subset declaration (#38617)

This commit is contained in:
James Hong
2019-09-26 15:22:12 -07:00
committed by Ben Lichtman
parent c72edfe745
commit 87be2f30cd
2 changed files with 23 additions and 8 deletions

View File

@@ -23,4 +23,4 @@ export default function useStore<S, A>(
inititalState: S,
actions: object,
initializers?: InitializerFunction<S, A>,
): () => [S, A];
): <NS, NA>(stateFunc?: (state: S) => NS, actionsFunc?: (state: A) => NA) => [NS, NA];

View File

@@ -1,13 +1,28 @@
import useStore, { Store, InitializerFunction } from 'use-global-hook';
import React = require('react');
interface state { value: string; }
interface associatedActions { setValue(value: string): void; }
const initializer: InitializerFunction<state, associatedActions> = (store: Store<state, associatedActions>) => {
store.actions.setValue("");
interface stateType {
value: string;
}
type setFunc = (value: string) => void;
interface associatedActionsType {
setValue: setFunc;
}
const initializer: InitializerFunction<stateType, associatedActionsType> = (store: Store<stateType, associatedActionsType>) => {
store.actions.setValue('');
store.state.value;
store.setState({ value: "string" });
store.setState({ value: 'string' });
};
useStore<state, associatedActions>(React, { value: "" }, {}, initializer); // $ExpectType () => [state, associatedActions]
useStore<state, associatedActions>(React, { value: "" }, {}); // $ExpectType () => [state, associatedActions]
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]
store<string, setFunc>((state: stateType) => state.value, (actions: associatedActionsType) => actions.setValue); // $ExpectType [string, setFunc]
useStore<stateType, associatedActionsType>(React, { value: '' }, {});