Skip to content

Commit 6da92b7

Browse files
committed
doc: explain how to use the generics to have ngOnChanges type safe
1 parent 09ec4c7 commit 6da92b7

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ import { getObservableLifecycle } from 'ngx-observable-lifecycle';
9494
changeDetection: ChangeDetectionStrategy.OnPush,
9595
})
9696
export class ChildComponent {
97-
@Input() input: number | undefined | null;
97+
@Input() input1: number | undefined | null;
98+
@Input() input2: string | undefined | null;
9899
99100
constructor() {
100101
const {
@@ -106,16 +107,32 @@ export class ChildComponent {
106107
ngAfterViewInit,
107108
ngAfterViewChecked,
108109
ngOnDestroy,
109-
} = getObservableLifecycle(this);
110+
} =
111+
// specifying the generics is only needed if you intend to
112+
// use the `ngOnChanges` observable, this way you'll have
113+
// typed input values instead of just a `SimpleChange`
114+
getObservableLifecycle<ChildComponent, 'input1' | 'input2'>(this);
110115
111-
ngOnChanges.subscribe(() => console.count('onChanges'));
112116
ngOnInit.subscribe(() => console.count('onInit'));
113117
ngDoCheck.subscribe(() => console.count('doCheck'));
114118
ngAfterContentInit.subscribe(() => console.count('afterContentInit'));
115119
ngAfterContentChecked.subscribe(() => console.count('afterContentChecked'));
116120
ngAfterViewInit.subscribe(() => console.count('afterViewInit'));
117121
ngAfterViewChecked.subscribe(() => console.count('afterViewChecked'));
118122
ngOnDestroy.subscribe(() => console.count('onDestroy'));
123+
124+
ngOnChanges.subscribe(changes => {
125+
console.count('onChanges');
126+
127+
// do note that we have a type safe object here for `changes`
128+
// with the inputs from our component and their associated values typed accordingly
129+
130+
changes.input1?.currentValue; // `number | null | undefined`
131+
changes.input1?.previousValue; // `number | null | undefined`
132+
133+
changes.input2?.currentValue; // `string | null | undefined`
134+
changes.input2?.previousValue; // `string | null | undefined`
135+
});
119136
}
120137
}
121138

src/app/child/child.component.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { getObservableLifecycle } from 'ngx-observable-lifecycle';
77
changeDetection: ChangeDetectionStrategy.OnPush,
88
})
99
export class ChildComponent {
10-
@Input() input: number | undefined | null;
10+
@Input() input1: number | undefined | null;
11+
@Input() input2: string | undefined | null;
1112

1213
constructor() {
1314
const {
@@ -19,15 +20,31 @@ export class ChildComponent {
1920
ngAfterViewInit,
2021
ngAfterViewChecked,
2122
ngOnDestroy,
22-
} = getObservableLifecycle(this);
23+
} =
24+
// specifying the generics is only needed if you intend to
25+
// use the `ngOnChanges` observable, this way you'll have
26+
// typed input values instead of just a `SimpleChange`
27+
getObservableLifecycle<ChildComponent, 'input1' | 'input2'>(this);
2328

24-
ngOnChanges.subscribe(() => console.count('onChanges'));
2529
ngOnInit.subscribe(() => console.count('onInit'));
2630
ngDoCheck.subscribe(() => console.count('doCheck'));
2731
ngAfterContentInit.subscribe(() => console.count('afterContentInit'));
2832
ngAfterContentChecked.subscribe(() => console.count('afterContentChecked'));
2933
ngAfterViewInit.subscribe(() => console.count('afterViewInit'));
3034
ngAfterViewChecked.subscribe(() => console.count('afterViewChecked'));
3135
ngOnDestroy.subscribe(() => console.count('onDestroy'));
36+
37+
ngOnChanges.subscribe(changes => {
38+
console.count('onChanges');
39+
40+
// do note that we have a type safe object here for `changes`
41+
// with the inputs from our component and their associated values typed accordingly
42+
43+
changes.input1?.currentValue; // `number | null | undefined`
44+
changes.input1?.previousValue; // `number | null | undefined`
45+
46+
changes.input2?.currentValue; // `string | null | undefined`
47+
changes.input2?.previousValue; // `string | null | undefined`
48+
});
3249
}
3350
}

0 commit comments

Comments
 (0)