11export type ListenerCallback = ( ) => void ;
22export 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 > > ;
44
55export type ChildFormMap < T , State , Error extends string > = {
66 [ Key in keyof T ] ?: ChildFormState < T , Key , State , Error > ;
@@ -10,7 +10,7 @@ export type DirtyMap<T> = {
1010 [ Key in keyof T ] ?: boolean ;
1111} ;
1212
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 ) ;
1414
1515export type ErrorMap < T , Error > = {
1616 [ Key in keyof T ] ?: ErrorType < T [ Key ] , Error > ;
@@ -329,7 +329,7 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
329329 let changed = this . childMap [ key ] ! . setErrors ( ( error as any ) ?? { } , true , false ) ;
330330 if ( ! changed ) return false ;
331331 } else {
332- this . childMap [ key ] ! . setErrors ( { } , true , false ) ;
332+ this . childMap [ key ] ! . setErrors ( { } as any , true , false ) ;
333333 }
334334 }
335335
@@ -345,7 +345,13 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
345345 * @param notifyChild Should this form notify the child form about this change?
346346 * @param notifyParent Should this form notify the parent form about this change?
347347 */
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+ }
349355 let keys = Object . keys ( this . errorMap ) ;
350356 let newKeys = Object . keys ( errors ) ;
351357 for ( let i = 0 ; i < newKeys . length ; i ++ ) {
0 commit comments