@@ -2,8 +2,8 @@ export type ListenerCallback = () => void;
22export type ListenerMap = { [ T in string ] ?: ListenerCallback } ;
33export type Validator < T , Error > = ( values : T ) => ErrorType < T , Error > | Promise < ErrorType < T , Error > > ;
44
5- export type ChildFormMap < T , State , Error extends string > = {
6- [ Key in keyof T ] ?: ChildFormState < T , Key , State , Error > ;
5+ export type ChildFormMap < T extends object , State , Error extends string > = {
6+ [ K in KeysOfType < T , object > ] ?: ChildFormState < T , K , State , Error > ;
77} ;
88
99export type DirtyMap < T > = {
@@ -19,6 +19,11 @@ export type ErrorMap<T, Error> = {
1919export type DefaultError = string ;
2020export type DefaultState = { isSubmitting : boolean } ;
2121
22+ export type FieldsOfType < T , Field > = {
23+ [ Key in keyof T as NonNullable < T [ Key ] > extends Field ? Key : never ] : T [ Key ] ;
24+ } ;
25+ export type KeysOfType < T extends FieldsOfType < any , Field > , Field > = keyof FieldsOfType < T , Field > ;
26+
2227function memberCopy < T > ( value : T ) : T {
2328 if ( Array . isArray ( value ) ) {
2429 return [ ...value ] as any ;
@@ -55,7 +60,7 @@ function comparePrimitiveObject<T>(a: T, b: T): boolean | undefined {
5560 return false ;
5661}
5762
58- export class FormState < T , State = DefaultState , Error extends string = DefaultError > {
63+ export class FormState < T extends object , State = DefaultState , Error extends string = DefaultError > {
5964 /**
6065 * The id of this form, for debugging purposes.
6166 */
@@ -174,7 +179,7 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
174179 this . dirtyMap [ key ] = dirty ;
175180
176181 if ( notifyChild ) {
177- let child = this . childMap [ key ] ;
182+ let child = this . childMap [ key as any ] ;
178183 if ( child ) {
179184 child . setValues ( value , validate , isDefault , true , false ) ;
180185 this . dirtyMap [ key ] = child . dirty ;
@@ -362,8 +367,8 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
362367 if ( ! error ) delete this . errorMap [ key ] ;
363368 else this . errorMap [ key ] = error ;
364369
365- if ( notifyChild && this . childMap [ key ] ) {
366- let changed = this . childMap [ key ] ! . setErrors ( error ?? ( { } as any ) , true , false ) ;
370+ if ( notifyChild && this . childMap [ key as any ] ) {
371+ let changed = this . childMap [ ( key as unknown ) as KeysOfType < T , object > ] ! . setErrors ( error ?? ( { } as any ) , true , false ) ;
367372 if ( ! changed && error !== undefined ) return false ;
368373 }
369374
@@ -446,7 +451,7 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
446451 this . _state = newState ;
447452
448453 let c = Object . keys ( this . values ) as ( keyof T ) [ ] ;
449- if ( notifyChild ) c . forEach ( ( e ) => this . childMap [ e ] ? .setState ( newState , true , false ) ) ;
454+ if ( notifyChild ) c . forEach ( ( e ) => ( this . childMap [ e as any ] as ChildFormState < T , any , State , Error > ) . setState ( newState , true , false ) ) ;
450455
451456 c . forEach ( ( e ) => this . fireListeners ( e ) ) ;
452457 this . fireAnyListeners ( ) ;
@@ -550,15 +555,15 @@ export class FormState<T, State = DefaultState, Error extends string = DefaultEr
550555 }
551556}
552557
553- export class ChildFormState < Parent , Key extends keyof Parent , ParentState , ParentError extends string > extends FormState <
554- NonNullable < Parent [ Key ] > ,
555- ParentState ,
556- ParentError
558+ export class ChildFormState < T extends FieldsOfType < any , object > , K extends KeysOfType < T , object > , State , Error extends string > extends FormState <
559+ NonNullable < T [ K ] > ,
560+ State ,
561+ Error
557562> {
558- public name : Key ;
559- public readonly parent : FormState < Parent , ParentState , ParentError > ;
563+ public name : K ;
564+ public readonly parent : FormState < T , State , Error > ;
560565
561- public constructor ( parent : FormState < Parent , ParentState , ParentError > , name : Key ) {
566+ public constructor ( parent : FormState < T , State , Error > , name : K ) {
562567 super (
563568 parent . values [ name ] ?? ( { } as any ) ,
564569 parent . defaultValues [ name ] ?? ( { } as any ) ,
@@ -570,4 +575,17 @@ export class ChildFormState<Parent, Key extends keyof Parent, ParentState, Paren
570575 this . parent = parent ;
571576 this . name = name ;
572577 }
578+
579+ // public setValueInternal<F extends keyof NonNullable<T[K]>>(
580+ // key: F,
581+ // value: T[K][F] | undefined,
582+ // dirty: boolean,
583+ // validate?: boolean,
584+ // isDefault: boolean = false,
585+ // notifyChild: boolean = true,
586+ // notifyParent: boolean = true,
587+ // fireAny: boolean = true
588+ // ) {
589+ // super.setValueInternal(key, value, dirty, validate, isDefault, notifyChild, notifyParent, fireAny);
590+ // }
573591}
0 commit comments