Skip to content

Commit a196e96

Browse files
committed
fix(selectvalue): should be typed
1 parent 8ec2495 commit a196e96

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,34 @@ export class HomeComponent {
279279
}
280280
```
281281
282+
## NgFormsManager Generic Type
283+
284+
`NgFormsManager` can take a generic type where you can define the forms shape. For example:
285+
286+
```ts
287+
export interface AppForms = {
288+
onboarding: {
289+
name: string;
290+
age: number;
291+
city: string;
292+
}
293+
}
294+
```
295+
296+
This will make sure that the queries are typed, and you don't make any mistakes in the form name.
297+
298+
```ts
299+
export class OnboardingComponent {
300+
constructor(private formsManager: NgFormsManager<AppForms>, private builder: FormBuilder) {}
301+
302+
ngOnInit() {
303+
this.formsManager.selectValue('onboarding').subscribe(value => {
304+
// value now typed as AppForms['onboarding']
305+
});
306+
}
307+
}
308+
```
309+
282310
## NgFormsManager Config
283311
284312
You can override the default config by passing the `NG_FORMS_MANAGER_CONFIG` provider:

projects/ngneat/forms-manager/src/lib/forms-manager.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export class NgFormsManager<FormsState = any> {
3030
return this.selectControl(formName, path).pipe(map(control => control.disabled));
3131
}
3232

33-
selectValue<T = any>(formName: keyof FormsState, path?: string): Observable<T> {
33+
selectValue<T extends keyof FormsState>(formName: T, path?: string): Observable<FormsState[T]> {
3434
return this.selectControl(formName, path).pipe(map(control => control.value));
3535
}
3636

37-
selectErrors(formName: keyof FormsState, path?: string): Observable<any> {
38-
return this.selectControl(formName, path).pipe(map(control => control.errors));
37+
selectErrors<T = any>(formName: keyof FormsState, path?: string): Observable<T> {
38+
return this.selectControl(formName, path).pipe(map(control => control.errors as T));
3939
}
4040

4141
/**
@@ -55,6 +55,7 @@ export class NgFormsManager<FormsState = any> {
5555
);
5656
}
5757

58+
// TODO: _AbstractControl should take a generic that should type the `value` property
5859
getControl(formName: keyof FormsState, path?: string): _AbstractControl {
5960
if (!path) {
6061
return this.getForm(formName);
@@ -68,6 +69,7 @@ export class NgFormsManager<FormsState = any> {
6869
return null;
6970
}
7071

72+
// TODO: _AbstractControl should take a generic that should type the `value` property
7173
selectForm(
7274
formName: keyof FormsState,
7375
options: { filterNil: true } = { filterNil: true }

0 commit comments

Comments
 (0)