Skip to content

Commit a106a3e

Browse files
committed
fix: πŸ› fix type of status$ property
βœ… Closes: #113
1 parent 21ce8f2 commit a106a3e

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

β€Žlibs/reactive-forms/src/lib/core.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ export function controlValueChanges$<T>(
2121
) as Observable<T>;
2222
}
2323

24-
export function controlStatus$(
24+
export type ControlState = 'VALID' | 'INVALID' | 'PENDING' | 'DISABLED';
25+
26+
export function controlStatus$<K extends 'disabled' | 'enabled' | 'status'>(
2527
control: AbstractControl,
26-
type: 'disabled' | 'enabled' | 'status'
27-
): Observable<boolean> {
28+
type: K
29+
): Observable<K extends 'status' ? ControlState : boolean> {
2830
return merge(
2931
defer(() => of(control[type])),
3032
control.statusChanges.pipe(
3133
map(() => control[type]),
3234
distinctUntilChanged()
3335
)
34-
) as Observable<boolean>;
36+
) as Observable<any>;
3537
}
3638

3739
export function enableControl(

β€Žlibs/reactive-forms/src/lib/form-array.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expectTypeOf } from 'expect-type';
22
import { Observable, of, Subject, Subscription } from 'rxjs';
33
import { FormControl, FormGroup } from '..';
4+
import { ControlState } from './core';
45
import { FormArray } from './form-array';
56

67
type Base = {
@@ -32,7 +33,7 @@ describe('FormArray Types', () => {
3233

3334
expectTypeOf(arr.disabled$).toEqualTypeOf<Observable<boolean>>();
3435
expectTypeOf(arr.enabled$).toEqualTypeOf<Observable<boolean>>();
35-
expectTypeOf(arr.status$).toEqualTypeOf<Observable<boolean>>();
36+
expectTypeOf(arr.status$).toEqualTypeOf<Observable<ControlState>>();
3637

3738
const first$ = arr.select((state) => {
3839
expectTypeOf(state).toEqualTypeOf<string[]>();

β€Žlibs/reactive-forms/src/lib/form-array.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import { DeepPartial } from './types';
2424
export class FormArray<
2525
T,
2626
Control extends AbstractControl = T extends Record<any, any>
27-
? FormGroup<ControlsOf<T>>
28-
: FormControl<T>
29-
> extends NgFormArray {
27+
? FormGroup<ControlsOf<T>>
28+
: FormControl<T>
29+
> extends NgFormArray {
3030
readonly value!: T[];
3131
readonly valueChanges!: Observable<T[]>;
3232

@@ -41,8 +41,8 @@ export class FormArray<
4141
.asObservable()
4242
.pipe(distinctUntilChanged());
4343
readonly value$ = controlValueChanges$<T[]>(this);
44-
readonly disabled$: Observable<boolean> = controlStatus$(this, 'disabled');
45-
readonly enabled$: Observable<boolean> = controlStatus$(this, 'enabled');
44+
readonly disabled$ = controlStatus$(this, 'disabled');
45+
readonly enabled$ = controlStatus$(this, 'enabled');
4646
readonly status$ = controlStatus$(this, 'status');
4747
readonly errors$ = controlErrorChanges$(
4848
this,

β€Žlibs/reactive-forms/src/lib/form-control.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Validators } from '@angular/forms';
22
import { expectTypeOf } from 'expect-type';
33
import { Observable, of, Subject, Subscription } from 'rxjs';
4+
import { ControlState } from './core';
45
import { FormControl } from './form-control';
56

67
describe('FormControl Functionality', () => {
@@ -201,7 +202,7 @@ describe('FormControl Types', () => {
201202

202203
expectTypeOf(control.disabled$).toEqualTypeOf<Observable<boolean>>();
203204
expectTypeOf(control.enabled$).toEqualTypeOf<Observable<boolean>>();
204-
expectTypeOf(control.status$).toEqualTypeOf<Observable<boolean>>();
205+
expectTypeOf(control.status$).toEqualTypeOf<Observable<ControlState>>();
205206

206207
// @ts-expect-error - should be typed
207208
control.reset(1);

β€Žlibs/reactive-forms/src/lib/form-control.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export class FormControl<T> extends NgFormControl {
3535
.asObservable()
3636
.pipe(distinctUntilChanged());
3737
readonly value$ = controlValueChanges$<T>(this);
38-
readonly disabled$: Observable<boolean> = controlStatus$(this, 'disabled');
39-
readonly enabled$: Observable<boolean> = controlStatus$(this, 'enabled');
38+
readonly disabled$ = controlStatus$(this, 'disabled');
39+
readonly enabled$ = controlStatus$(this, 'enabled');
4040
readonly status$ = controlStatus$(this, 'status');
4141
readonly errors$ = controlErrorChanges$(
4242
this,

β€Žlibs/reactive-forms/src/lib/form-group.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AbstractControl } from '@angular/forms';
66
import { Observable, of, Subject, Subscription } from 'rxjs';
77
import { ControlsOf } from '..';
88
import { ValuesOf } from './types';
9+
import { ControlState } from './core';
910

1011
const createGroup = () => {
1112
return new FormGroup(
@@ -316,7 +317,7 @@ describe('FormGroup Types', () => {
316317

317318
expectTypeOf(group.disabled$).toEqualTypeOf<Observable<boolean>>();
318319
expectTypeOf(group.enabled$).toEqualTypeOf<Observable<boolean>>();
319-
expectTypeOf(group.status$).toEqualTypeOf<Observable<boolean>>();
320+
expectTypeOf(group.status$).toEqualTypeOf<Observable<ControlState>>();
320321

321322
const name$ = group.select((state) => {
322323
expectTypeOf(state).toEqualTypeOf<Base>();

β€Žlibs/reactive-forms/src/lib/form-group.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export class FormGroup<T extends Record<string, any>> extends NgFormGroup {
3636
.asObservable()
3737
.pipe(distinctUntilChanged());
3838
readonly value$ = controlValueChanges$<ValuesOf<T>>(this);
39-
readonly disabled$: Observable<boolean> = controlStatus$(this, 'disabled');
40-
readonly enabled$: Observable<boolean> = controlStatus$(this, 'enabled');
39+
readonly disabled$ = controlStatus$(this, 'disabled');
40+
readonly enabled$ = controlStatus$(this, 'enabled');
4141
readonly status$ = controlStatus$(this, 'status');
4242
readonly errors$ = controlErrorChanges$(
4343
this,

0 commit comments

Comments
Β (0)