Skip to content

Commit 94b8c58

Browse files
TypeScript null detection obscurity
Even though control.parent is checked for having a value, it is still flagged as potentially null. Fixed by capturing the variable separately so TS detects it as having a value within the closure.
1 parent 4efc21a commit 94b8c58

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

angular-reactive-validation/src/get-control-path.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ import { AbstractControl } from '@angular/forms';
33
/**
44
* Given a control, returns a string representation of the property path to
55
* this control. Thus, for a FormControl 'firstName', that is part of a
6-
* FormGroup references to as 'name', this function will return: 'name.firstName'.
6+
* FormGroup named 'name', this function will return: 'name.firstName'.
77
*
88
* Note that FormArray indexes are also put in the path, e.g.: 'person.0.name.firstName'.
99
*/
1010
export function getControlPath(control: AbstractControl): string {
11-
if (control.parent) {
12-
let path = getControlPath(control.parent);
11+
const parentControl = control.parent;
12+
if (parentControl) {
13+
let path = getControlPath(parentControl);
1314
if (path) {
1415
path += '.';
1516
}
16-
return path + Object.keys(control.parent.controls).find(key => {
17-
const controls = control.parent.controls;
17+
return path + Object.keys(parentControl.controls).find(key => {
18+
const controls = parentControl.controls;
1819
if (Array.isArray(controls)) {
1920
return controls[Number(key)] === control;
2021
} else {

0 commit comments

Comments
 (0)