angular-cn/packages/core/schematics/migrations/abstract-control-parent
Kristiyan Kostadinov aeec223db1 feat(forms): add migration for AbstractControl.parent accesses (#39009)
As of #32671, the type of `AbstractControl.parent` can be null which can cause
compilation errors in existing apps. These changes add a migration that will append
non-null assertions to existing unsafe accesses.

````
// Before
console.log(control.parent.value);

// After
console.log(control.parent!.value);
```

The migration also tries its best to avoid cases where the non-null assertions aren't
necessary (e.g. if the `parent` was null checked already).

PR Close #39009
2020-10-06 13:55:25 -07:00
..
BUILD.bazel feat(forms): add migration for AbstractControl.parent accesses (#39009) 2020-10-06 13:55:25 -07:00
README.md feat(forms): add migration for AbstractControl.parent accesses (#39009) 2020-10-06 13:55:25 -07:00
index.ts feat(forms): add migration for AbstractControl.parent accesses (#39009) 2020-10-06 13:55:25 -07:00
util.ts feat(forms): add migration for AbstractControl.parent accesses (#39009) 2020-10-06 13:55:25 -07:00

README.md

AbstractControl.parent migration

As of Angular v11, the type of AbstractControl.parent can be null. This migration automatically identifies usages and adds non-null assertions.

Before

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component()
export class MyComponent {
  private _control = new FormControl();

  getParentValue() {
    return this._control.parent.value; // <- Compilation error in v11.
  }
}

After

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';

@Component()
export class MyComponent {
  private _control = new FormControl();

  getParentValue() {
    return this._control.parent!.value; // <- Non-null assertion added during the migration.
  }
}