Skip to content

Commit 1bc0c0b

Browse files
authored
Merge pull request #35 from nullgr/feature/TSvalidateTypingImproved
validate method TS typing improved
2 parents abd3300 + 2fd66c1 commit 1bc0c0b

2 files changed

Lines changed: 10 additions & 14 deletions

File tree

build/index.d.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type FieldsDescription = {
1010
[key: string]: RuleData | RuleData[];
1111
};
1212

13+
type updaterFunction<T > = (prevState: Readonly<T>) => T;
14+
1315
export default class Validator<State> {
1416
constructor(fields: FieldsDescription, validationStorageName?: string);
1517

@@ -25,18 +27,14 @@ export default class Validator<State> {
2527
addValidation(state: State, showErrorsOnStart?: boolean): Readonly<State>;
2628

2729
validate(
28-
stateUpdates: (prevState: Readonly<State>) => State,
30+
stateUpdates?: Partial<State> | updaterFunction<State> | null,
2931
showErrors?: boolean
3032
): ((prevState: Readonly<State>) => State);
3133

32-
validate(
33-
stateUpdates?: { [key in keyof State]: any } | null,
34-
showErrors?: boolean
35-
): Readonly<State>;
36-
37-
getErrors(state: State): { [key in keyof State]: any };
34+
getErrors(state: State): { [key in keyof State]: string };
3835

3936
isFormValid(state: State): boolean;
4037

4138
isFieldValid(state: State, fieldName: string): boolean;
4239
}
40+

types/index.d.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type FieldsDescription = {
1010
[key: string]: RuleData | RuleData[];
1111
};
1212

13+
type updaterFunction<T> = (prevState: Readonly<T>) => T;
14+
1315
export default class Validator<State> {
1416
constructor(fields: FieldsDescription, validationStorageName?: string);
1517

@@ -25,18 +27,14 @@ export default class Validator<State> {
2527
addValidation(state: State, showErrorsOnStart?: boolean): Readonly<State>;
2628

2729
validate(
28-
stateUpdates: (prevState: Readonly<State>) => State,
30+
stateUpdates?: Partial<State> | updaterFunction<State> | null,
2931
showErrors?: boolean
3032
): ((prevState: Readonly<State>) => State);
3133

32-
validate(
33-
stateUpdates?: { [key in keyof State]: any } | null,
34-
showErrors?: boolean
35-
): Readonly<State>;
36-
37-
getErrors(state: State): { [key in keyof State]: any };
34+
getErrors(state: State): { [key in keyof State]: string };
3835

3936
isFormValid(state: State): boolean;
4037

4138
isFieldValid(state: State, fieldName: string): boolean;
4239
}
40+

0 commit comments

Comments
 (0)