1
1
export type ListenerCallback = ( ) => void ;
2
2
export type ListenerMap = { [ T in string ] ?: ListenerCallback } ;
3
- export type Validator < T , Error > = ( values : T ) => ErrorMap < T , Error > | Promise < ErrorMap < T , Error > > ;
3
+ export type Validator < T , Error > = ( values : T ) => ErrorType < T , Error > | Promise < ErrorType < T , Error > > ;
4
4
5
5
export type ChildFormMap < T , State , Error extends string > = {
6
6
[ Key in keyof T ] ?: ChildFormState < T , Key , State , Error > ;
@@ -10,7 +10,7 @@ export type DirtyMap<T> = {
10
10
[ Key in keyof T ] ?: boolean ;
11
11
} ;
12
12
13
- export type ErrorType < T , Error > = T extends object ? ErrorMap < T , Error > | Error : Error ;
13
+ export type ErrorType < T , Error > = Error | ( T extends { } ? ErrorMap < T , Error > : never ) ;
14
14
15
15
export type ErrorMap < T , Error > = {
16
16
[ Key in keyof T ] ?: ErrorType < T [ Key ] , Error > ;
@@ -329,7 +329,7 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
329
329
let changed = this . childMap [ key ] ! . setErrors ( ( error as any ) ?? { } , true , false ) ;
330
330
if ( ! changed ) return false ;
331
331
} else {
332
- this . childMap [ key ] ! . setErrors ( { } , true , false ) ;
332
+ this . childMap [ key ] ! . setErrors ( { } as any , true , false ) ;
333
333
}
334
334
}
335
335
@@ -345,7 +345,13 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
345
345
* @param notifyChild Should this form notify the child form about this change?
346
346
* @param notifyParent Should this form notify the parent form about this change?
347
347
*/
348
- public setErrors ( errors : ErrorMap < T , Error > , notifyChild : boolean = true , notifyParent : boolean = true ) {
348
+ public setErrors ( errors : ErrorType < T , Error > , notifyChild : boolean = true , notifyParent : boolean = true ) {
349
+ if ( typeof errors === "string" ) {
350
+ if ( notifyParent && this instanceof ChildFormState ) {
351
+ this . parent . setError ( this . name , errors , false , true ) ;
352
+ }
353
+ return ;
354
+ }
349
355
let keys = Object . keys ( this . errorMap ) ;
350
356
let newKeys = Object . keys ( errors ) ;
351
357
for ( let i = 0 ; i < newKeys . length ; i ++ ) {
0 commit comments