Skip to content

Commit

Permalink
fix: improve useStateValidator and useMultiStateValidator typings.
Browse files Browse the repository at this point in the history
Now it is compatible with TS 4.x.x
  • Loading branch information
xobotyi committed Jan 8, 2021
1 parent ebc7094 commit acff81d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/useMultiStateValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { StateValidator, UseStateValidatorReturn, ValidityState } from './useSta
export type MultiStateValidatorStates = any[] | { [p: string]: any } | { [p: number]: any };
export type MultiStateValidator<V extends ValidityState, S extends MultiStateValidatorStates> = StateValidator<V, S>;

export function useMultiStateValidator<V extends ValidityState, S extends MultiStateValidatorStates, I extends V>(
export function useMultiStateValidator<V extends ValidityState, S extends MultiStateValidatorStates>(
states: S,
validator: MultiStateValidator<V, S>,
initialValidity: I = [undefined] as I
initialValidity: V = [undefined] as V
): UseStateValidatorReturn<V> {
if (typeof states !== 'object') {
throw new Error('states expected to be an object or array, got ' + typeof states);
Expand Down
6 changes: 3 additions & 3 deletions src/useStateValidator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dispatch, SetStateAction, useCallback, useEffect, useRef, useState } from 'react';

export type ValidityState = [boolean | undefined, ...any[]];
export type ValidityState = [boolean | undefined, ...any[]] | [undefined];

export interface StateValidator<V, S> {
(state: S): V;
Expand All @@ -10,10 +10,10 @@ export interface StateValidator<V, S> {

export type UseStateValidatorReturn<V> = [V, () => void];

export default function useStateValidator<V extends ValidityState, S, I extends V>(
export default function useStateValidator<V extends ValidityState, S>(
state: S,
validator: StateValidator<V, S>,
initialState: I = [undefined] as I
initialState: V = [undefined] as V
): UseStateValidatorReturn<V> {
const validatorInner = useRef(validator);
const stateInner = useRef(state);
Expand Down

0 comments on commit acff81d

Please sign in to comment.