Skip to content
This repository has been archived by the owner on May 20, 2022. It is now read-only.

Commit

Permalink
Update types to not allow undefined on store state
Browse files Browse the repository at this point in the history
Update types to not allow undefined on store state.
Currently using undefined as the store initial value leads to the initial value being an empty object, which is not correct.
So by restricting the type to not be undefined, users can just use null and that will work fine.
  • Loading branch information
kristapsPelna authored and jhonnymichel committed Oct 21, 2020
1 parent 8b2e53c commit cc27aa7
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions types/index.d.ts
@@ -1,4 +1,6 @@
declare module 'react-hookstore' {
type NonUndefined<T> = T extends undefined ? never : T;

type StateCallback<TState> = (state: TState) => void;

type ReducerType<TState, TPayload = any> = (state: TState, payload: TPayload) => TState;
Expand Down Expand Up @@ -32,11 +34,11 @@ declare module 'react-hookstore' {
dispatch<TPayload>(payload: TPayload, callback?: StateCallback<TState>): void;
}

export function createStore<TState, TPayload = any>(name: string, state: TState, reducer: ReducerType<TState, TPayload>): ReducerStoreInterface<TState, TPayload>;
export function createStore<TState, TPayload = any>(name: string, state: NonUndefined<TState>, reducer: ReducerType<TState, TPayload>): ReducerStoreInterface<TState, TPayload>;

export function createStore<TState>(name: string, state: TState): StateStoreInterface<TState>;
export function createStore<TState>(name: string, state: NonUndefined<TState>): StateStoreInterface<TState>;

export function createStore<TState>(name: string, state: TState, reducer: ReducerType<TState>): ReducerStoreInterface<TState>;
export function createStore<TState>(name: string, state: NonUndefined<TState>, reducer: ReducerType<TState>): ReducerStoreInterface<TState>;

export function getStoreByName<TState, TPayload = any>(name: string): StateStoreInterface<TState> | ReducerStoreInterface<TState>;

Expand Down

0 comments on commit cc27aa7

Please sign in to comment.