@@ -75,62 +75,84 @@ function abstractControlReducer<TValue>(state: AbstractControlState<TValue>, act
7575 return isGroupState ( state ) ? formGroupReducer ( state as any , action ) as any : formControlReducer ( state as any , action ) ;
7676}
7777
78- export function setValue < TValue extends FormControlValueTypes > ( value : TValue ) : ProjectFn < FormControlState < TValue > > ;
79- export function setValue < TValue extends KeyValue > ( value : TValue ) : ProjectFn < FormGroupState < TValue > > ;
80- export function setValue < TValue > ( value : TValue ) {
81- return ( state : AbstractControlState < TValue > ) => abstractControlReducer ( state , new SetValueAction ( state . id , value ) ) ;
78+ function ensureState < TValue > ( state : AbstractControlState < TValue > ) {
79+ if ( ! state ) {
80+ throw new Error ( 'state must not be undefined!' ) ;
81+ }
82+
83+ return state ;
84+ }
85+
86+ // TODO: enable all overloads once conditional mapped types are implemented (https://github.com/Microsoft/TypeScript/issues/12424)
87+
88+ // export function setValue<TValue extends FormControlValueTypes>(value: TValue): ProjectFn<FormControlState<TValue>>;
89+ // export function setValue<TValue extends KeyValue>(value: TValue): ProjectFn<FormGroupState<TValue>>;
90+ export function setValue < TValue > ( value : TValue ) : ProjectFn < AbstractControlState < TValue > > ;
91+ export function setValue < TValue > ( value : TValue , state : AbstractControlState < TValue > ) : AbstractControlState < TValue > ;
92+ export function setValue < TValue > ( value : TValue , state ?: AbstractControlState < TValue > ) {
93+ if ( ! ! state ) {
94+ return abstractControlReducer ( state , new SetValueAction ( state . id , value ) ) ;
95+ }
96+
97+ return ( s : AbstractControlState < TValue > ) => setValue ( value , ensureState ( s ) ) ;
8298}
8399
84- export function validate < TValue extends FormControlValueTypes > ( validatorFn : ( value : TValue ) => ValidationErrors ) : ProjectFn < FormControlState < TValue > > ;
85- export function validate < TValue extends KeyValue > ( validatorFn : ( value : TValue ) => ValidationErrors ) : ProjectFn < FormGroupState < TValue > > ;
86- export function validate < TValue > ( validatorFn : ( value : TValue ) => ValidationErrors ) {
87- return ( state : AbstractControlState < TValue > ) => abstractControlReducer ( state , new SetErrorsAction ( state . id , validatorFn ( state . value ) ) ) ;
100+ // export function validate<TValue extends FormControlValueTypes>(validatorFn: (value: TValue) => ValidationErrors): ProjectFn<FormControlState<TValue>>;
101+ // export function validate<TValue extends KeyValue>(validatorFn: (value: TValue) => ValidationErrors): ProjectFn<FormGroupState<TValue>>;
102+ export function validate < TValue > ( validatorFn : ( value : TValue ) => ValidationErrors ) : ProjectFn < AbstractControlState < TValue > > ;
103+ export function validate < TValue > ( validatorFn : ( value : TValue ) => ValidationErrors , state : AbstractControlState < TValue > ) : AbstractControlState < TValue > ;
104+ export function validate < TValue > ( validatorFn : ( value : TValue ) => ValidationErrors , state ?: AbstractControlState < TValue > ) {
105+ if ( ! ! state ) {
106+ return abstractControlReducer ( state , new SetErrorsAction ( state . id , validatorFn ( state . value ) ) ) ;
107+ }
108+
109+ return ( s : AbstractControlState < TValue > ) => validate ( validatorFn , ensureState ( s ) ) ;
88110}
89111
90- export function enable < TValue extends FormControlValueTypes > ( state : FormControlState < TValue > ) : FormControlState < TValue > ;
91- export function enable < TValue extends KeyValue > ( state : FormGroupState < TValue > ) : FormGroupState < TValue > ;
112+ // export function enable<TValue extends FormControlValueTypes>(state: FormControlState<TValue>): FormControlState<TValue>;
113+ // export function enable<TValue extends KeyValue>(state: FormGroupState<TValue>): FormGroupState<TValue>;
92114export function enable < TValue > ( state : AbstractControlState < TValue > ) {
93115 return abstractControlReducer ( state , new EnableAction ( state . id ) ) ;
94116}
95117
96- export function disable < TValue extends FormControlValueTypes > ( state : FormControlState < TValue > ) : FormControlState < TValue > ;
97- export function disable < TValue extends KeyValue > ( state : FormGroupState < TValue > ) : FormGroupState < TValue > ;
118+ // export function disable<TValue extends FormControlValueTypes>(state: FormControlState<TValue>): FormControlState<TValue>;
119+ // export function disable<TValue extends KeyValue>(state: FormGroupState<TValue>): FormGroupState<TValue>;
98120export function disable < TValue > ( state : AbstractControlState < TValue > ) {
99121 return abstractControlReducer ( state , new DisableAction ( state . id ) ) ;
100122}
101123
102- export function markAsDirty < TValue extends FormControlValueTypes > ( state : FormControlState < TValue > ) : FormControlState < TValue > ;
103- export function markAsDirty < TValue extends KeyValue > ( state : FormGroupState < TValue > ) : FormGroupState < TValue > ;
124+ // export function markAsDirty<TValue extends FormControlValueTypes>(state: FormControlState<TValue>): FormControlState<TValue>;
125+ // export function markAsDirty<TValue extends KeyValue>(state: FormGroupState<TValue>): FormGroupState<TValue>;
104126export function markAsDirty < TValue > ( state : AbstractControlState < TValue > ) {
105127 return abstractControlReducer ( state , new MarkAsDirtyAction ( state . id ) ) ;
106128}
107129
108- export function markAsPristine < TValue extends FormControlValueTypes > ( state : FormControlState < TValue > ) : FormControlState < TValue > ;
109- export function markAsPristine < TValue extends KeyValue > ( state : FormGroupState < TValue > ) : FormGroupState < TValue > ;
130+ // export function markAsPristine<TValue extends FormControlValueTypes>(state: FormControlState<TValue>): FormControlState<TValue>;
131+ // export function markAsPristine<TValue extends KeyValue>(state: FormGroupState<TValue>): FormGroupState<TValue>;
110132export function markAsPristine < TValue > ( state : AbstractControlState < TValue > ) {
111133 return abstractControlReducer ( state , new MarkAsPristineAction ( state . id ) ) ;
112134}
113135
114- export function markAsTouched < TValue extends FormControlValueTypes > ( state : FormControlState < TValue > ) : FormControlState < TValue > ;
115- export function markAsTouched < TValue extends KeyValue > ( state : FormGroupState < TValue > ) : FormGroupState < TValue > ;
136+ // export function markAsTouched<TValue extends FormControlValueTypes>(state: FormControlState<TValue>): FormControlState<TValue>;
137+ // export function markAsTouched<TValue extends KeyValue>(state: FormGroupState<TValue>): FormGroupState<TValue>;
116138export function markAsTouched < TValue > ( state : AbstractControlState < TValue > ) {
117139 return abstractControlReducer ( state , new MarkAsTouchedAction ( state . id ) ) ;
118140}
119141
120- export function markAsUntouched < TValue extends FormControlValueTypes > ( state : FormControlState < TValue > ) : FormControlState < TValue > ;
121- export function markAsUntouched < TValue extends KeyValue > ( state : FormGroupState < TValue > ) : FormGroupState < TValue > ;
142+ // export function markAsUntouched<TValue extends FormControlValueTypes>(state: FormControlState<TValue>): FormControlState<TValue>;
143+ // export function markAsUntouched<TValue extends KeyValue>(state: FormGroupState<TValue>): FormGroupState<TValue>;
122144export function markAsUntouched < TValue > ( state : AbstractControlState < TValue > ) {
123145 return abstractControlReducer ( state , new MarkAsUntouchedAction ( state . id ) ) ;
124146}
125147
126- export function markAsSubmitted < TValue extends FormControlValueTypes > ( state : FormControlState < TValue > ) : FormControlState < TValue > ;
127- export function markAsSubmitted < TValue extends KeyValue > ( state : FormGroupState < TValue > ) : FormGroupState < TValue > ;
148+ // export function markAsSubmitted<TValue extends FormControlValueTypes>(state: FormControlState<TValue>): FormControlState<TValue>;
149+ // export function markAsSubmitted<TValue extends KeyValue>(state: FormGroupState<TValue>): FormGroupState<TValue>;
128150export function markAsSubmitted < TValue > ( state : AbstractControlState < TValue > ) {
129151 return abstractControlReducer ( state , new MarkAsSubmittedAction ( state . id ) ) ;
130152}
131153
132- export function markAsUnsubmitted < TValue extends FormControlValueTypes > ( state : FormControlState < TValue > ) : FormControlState < TValue > ;
133- export function markAsUnsubmitted < TValue extends KeyValue > ( state : FormGroupState < TValue > ) : FormGroupState < TValue > ;
154+ // export function markAsUnsubmitted<TValue extends FormControlValueTypes>(state: FormControlState<TValue>): FormControlState<TValue>;
155+ // export function markAsUnsubmitted<TValue extends KeyValue>(state: FormGroupState<TValue>): FormGroupState<TValue>;
134156export function markAsUnsubmitted < TValue > ( state : AbstractControlState < TValue > ) {
135157 return abstractControlReducer ( state , new MarkAsUnsubmittedAction ( state . id ) ) ;
136158}
0 commit comments