Skip to content

Commit 713497f

Browse files
committed
fix: remove generic type constraints from directives to prevent errors during angular template type-checking
1 parent 9a55ddb commit 713497f

File tree

5 files changed

+34
-22
lines changed

5 files changed

+34
-22
lines changed

src/control/directive.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,17 @@ class ControlValueAccessorAdapter implements FormViewAdapter {
5151
}
5252
}
5353

54+
export type NgrxFormControlValueType<TStateValue> = TStateValue extends FormControlValueTypes ? TStateValue : never;
55+
5456
@Directive({
5557
// tslint:disable-next-line:directive-selector
5658
selector: ':not([ngrxFormsAction])[ngrxFormControlState]',
5759
})
58-
export class NgrxFormControlDirective<TStateValue extends FormControlValueTypes, TViewValue = TStateValue> implements AfterViewInit, OnInit {
60+
export class NgrxFormControlDirective<TStateValue, TViewValue = TStateValue> implements AfterViewInit, OnInit {
5961
private isInitialized = false;
6062
private focusTrackingIsEnabled = false;
6163

62-
@Input() set ngrxFormControlState(newState: FormControlState<TStateValue>) {
64+
@Input() set ngrxFormControlState(newState: FormControlState<NgrxFormControlValueType<TStateValue>>) {
6365
if (!newState) {
6466
throw new Error('The control state must not be undefined!');
6567
}
@@ -92,7 +94,7 @@ export class NgrxFormControlDirective<TStateValue extends FormControlValueTypes,
9294
return this.state && this.state.isFocused ? '' : null;
9395
}
9496

95-
state: FormControlState<TStateValue>;
97+
state: FormControlState<NgrxFormControlValueType<TStateValue>>;
9698

9799
private viewAdapter: FormViewAdapter;
98100

@@ -128,7 +130,10 @@ export class NgrxFormControlDirective<TStateValue extends FormControlValueTypes,
128130
: selectViewAdapter(viewAdapters);
129131
}
130132

131-
updateViewIfControlIdChanged(newState: FormControlState<TStateValue>, oldState: FormControlState<TStateValue> | undefined) {
133+
updateViewIfControlIdChanged(
134+
newState: FormControlState<NgrxFormControlValueType<TStateValue>>,
135+
oldState: FormControlState<NgrxFormControlValueType<TStateValue>> | undefined,
136+
) {
132137
if (oldState && newState.id === oldState.id) {
133138
return;
134139
}
@@ -141,7 +146,10 @@ export class NgrxFormControlDirective<TStateValue extends FormControlValueTypes,
141146
}
142147
}
143148

144-
updateViewIfValueChanged(newState: FormControlState<TStateValue>, _: FormControlState<TStateValue> | undefined) {
149+
updateViewIfValueChanged(
150+
newState: FormControlState<NgrxFormControlValueType<TStateValue>>,
151+
_: FormControlState<NgrxFormControlValueType<TStateValue>> | undefined,
152+
) {
145153
if (newState.value === this.stateValue) {
146154
return;
147155
}
@@ -151,7 +159,10 @@ export class NgrxFormControlDirective<TStateValue extends FormControlValueTypes,
151159
this.viewAdapter.setViewValue(this.viewValue);
152160
}
153161

154-
updateViewIfIsDisabledChanged(newState: FormControlState<TStateValue>, oldState: FormControlState<TStateValue> | undefined) {
162+
updateViewIfIsDisabledChanged(
163+
newState: FormControlState<NgrxFormControlValueType<TStateValue>>,
164+
oldState: FormControlState<NgrxFormControlValueType<TStateValue>> | undefined,
165+
) {
155166
if (!this.viewAdapter.setIsDisabled) {
156167
return;
157168
}
@@ -163,7 +174,10 @@ export class NgrxFormControlDirective<TStateValue extends FormControlValueTypes,
163174
this.viewAdapter.setIsDisabled(newState.isDisabled);
164175
}
165176

166-
updateViewIfIsFocusedChanged(newState: FormControlState<TStateValue>, oldState: FormControlState<TStateValue> | undefined) {
177+
updateViewIfIsFocusedChanged(
178+
newState: FormControlState<NgrxFormControlValueType<TStateValue>>,
179+
oldState: FormControlState<NgrxFormControlValueType<TStateValue>> | undefined,
180+
) {
167181
if (!this.focusTrackingIsEnabled) {
168182
return;
169183
}
@@ -179,7 +193,7 @@ export class NgrxFormControlDirective<TStateValue extends FormControlValueTypes,
179193
}
180194
}
181195

182-
protected dispatchAction(action: Actions<TStateValue>) {
196+
protected dispatchAction(action: Actions<NgrxFormControlValueType<TStateValue>>) {
183197
if (this.actionsSubject !== null) {
184198
this.actionsSubject.next(action);
185199
} else {
@@ -208,7 +222,7 @@ export class NgrxFormControlDirective<TStateValue extends FormControlValueTypes,
208222
const dispatchSetValueAction = () => {
209223
this.stateValue = this.ngrxValueConverter.convertViewToStateValue(this.viewValue);
210224
if (this.stateValue !== this.state.value) {
211-
this.dispatchAction(new SetValueAction(this.state.id, this.stateValue));
225+
this.dispatchAction(new SetValueAction(this.state.id, this.stateValue as NgrxFormControlValueType<TStateValue>));
212226

213227
dispatchMarkAsDirtyAction();
214228
}

src/control/local-state-directive.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@ import {
1111
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
1212

1313
import { Actions } from '../actions';
14-
import { FormControlValueTypes } from '../state';
1514
import { FormViewAdapter, NGRX_FORM_VIEW_ADAPTER } from '../view-adapter/view-adapter';
16-
import { Document, NgrxFormControlDirective } from './directive';
15+
import { Document, NgrxFormControlDirective, NgrxFormControlValueType } from './directive';
1716

1817
@Directive({
1918
// tslint:disable-next-line:directive-selector
2019
selector: '[ngrxFormControlState][ngrxFormsAction]',
2120
})
22-
export class NgrxLocalFormControlDirective<TStateValue extends FormControlValueTypes, TViewValue = TStateValue>
21+
export class NgrxLocalFormControlDirective<TStateValue, TViewValue = TStateValue>
2322
extends NgrxFormControlDirective<TStateValue, TViewValue> {
2423

25-
@Output() ngrxFormsAction = new EventEmitter<Actions<TStateValue>>();
24+
@Output() ngrxFormsAction = new EventEmitter<Actions<NgrxFormControlValueType<TStateValue>>>();
2625

2726
constructor(
2827
el: ElementRef,
@@ -33,7 +32,7 @@ export class NgrxLocalFormControlDirective<TStateValue extends FormControlValueT
3332
super(el, dom, null, viewAdapters, valueAccessors);
3433
}
3534

36-
protected dispatchAction(action: Actions<TStateValue>) {
35+
protected dispatchAction(action: Actions<NgrxFormControlValueType<TStateValue>>) {
3736
this.ngrxFormsAction.emit(action);
3837
}
3938
}

src/group/directive.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ interface CustomEvent extends Event { }
1313
// tslint:disable-next-line:directive-selector
1414
selector: 'form:not([ngrxFormsAction])[ngrxFormState]',
1515
})
16-
export class NgrxFormDirective<TValue extends { [key: string]: any }> implements OnInit {
16+
export class NgrxFormDirective<TStateValue> implements OnInit {
1717
// tslint:disable-next-line:no-input-rename
18-
@Input('ngrxFormState') state: FormGroupState<TValue>;
18+
@Input('ngrxFormState') state: FormGroupState<TStateValue>;
1919

2020
constructor(
2121
@Optional() @Inject(ActionsSubject) private actionsSubject: ActionsSubject | null
2222
) {
2323
this.actionsSubject = actionsSubject;
2424
}
2525

26-
protected dispatchAction(action: Actions<TValue>) {
26+
protected dispatchAction(action: Actions<TStateValue>) {
2727
if (this.actionsSubject !== null) {
2828
this.actionsSubject.next(action);
2929
} else {

src/group/local-state-directive.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import { NgrxFormDirective } from './directive';
77
// tslint:disable-next-line:directive-selector
88
selector: 'form[ngrxFormState][ngrxFormsAction]',
99
})
10-
export class NgrxLocalFormDirective<TValue extends { [key: string]: any }>
11-
extends NgrxFormDirective<TValue> {
10+
export class NgrxLocalFormDirective<TStateValue> extends NgrxFormDirective<TStateValue> {
1211

13-
@Output() ngrxFormsAction = new EventEmitter<Actions<TValue>>();
12+
@Output() ngrxFormsAction = new EventEmitter<Actions<TStateValue>>();
1413

1514
constructor() {
1615
super(null);
1716
}
1817

19-
protected dispatchAction(action: Actions<TValue>) {
18+
protected dispatchAction(action: Actions<TStateValue>) {
2019
this.ngrxFormsAction.emit(action);
2120
}
2221
}

src/ngrx-forms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export { NgrxRangeViewAdapter } from './view-adapter/range';
7373
export { NgrxSelectViewAdapter, NgrxSelectOption } from './view-adapter/select';
7474
export { NgrxSelectMultipleViewAdapter, NgrxSelectMultipleOption } from './view-adapter/select-multiple';
7575

76-
export { NgrxFormControlDirective, NGRX_UPDATE_ON_TYPE } from './control/directive';
76+
export { NgrxFormControlDirective, NgrxFormControlValueType, NGRX_UPDATE_ON_TYPE } from './control/directive';
7777
export { NgrxLocalFormControlDirective } from './control/local-state-directive';
7878
export { NgrxFormDirective } from './group/directive';
7979
export { NgrxLocalFormDirective } from './group/local-state-directive';

0 commit comments

Comments
 (0)