Skip to content

Commit

Permalink
TypeScript null detection obscurity
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davidwalschots committed Nov 17, 2020
1 parent 4efc21a commit 94b8c58
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions angular-reactive-validation/src/get-control-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import { AbstractControl } from '@angular/forms';
/**
* Given a control, returns a string representation of the property path to
* this control. Thus, for a FormControl 'firstName', that is part of a
* FormGroup references to as 'name', this function will return: 'name.firstName'.
* FormGroup named 'name', this function will return: 'name.firstName'.
*
* Note that FormArray indexes are also put in the path, e.g.: 'person.0.name.firstName'.
*/
export function getControlPath(control: AbstractControl): string {
if (control.parent) {
let path = getControlPath(control.parent);
const parentControl = control.parent;
if (parentControl) {
let path = getControlPath(parentControl);
if (path) {
path += '.';
}
return path + Object.keys(control.parent.controls).find(key => {
const controls = control.parent.controls;
return path + Object.keys(parentControl.controls).find(key => {
const controls = parentControl.controls;
if (Array.isArray(controls)) {
return controls[Number(key)] === control;
} else {
Expand Down

0 comments on commit 94b8c58

Please sign in to comment.