refactor(elements): remove unnecessary non-null assertions and as any type-casts (#36161)

This commit removes some unnecessary non-null assertions (`!`) and
`as any` type-casts from the `elements` package.

PR Close #36161
This commit is contained in:
George Kalpakas 2020-06-05 20:14:16 +03:00 committed by atscott
parent 85b6c94cc6
commit 569d1ef583

View File

@ -48,8 +48,7 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
events!: Observable<NgElementStrategyEvent>; events!: Observable<NgElementStrategyEvent>;
/** Reference to the component that was created on connect. */ /** Reference to the component that was created on connect. */
// TODO(issue/24571): remove '!'. private componentRef: ComponentRef<any>|null = null;
private componentRef!: ComponentRef<any>|null;
/** Changes that have been made to the component ref since the last time onChanges was called. */ /** Changes that have been made to the component ref since the last time onChanges was called. */
private inputChanges: SimpleChanges|null = null; private inputChanges: SimpleChanges|null = null;
@ -86,7 +85,7 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
return; return;
} }
if (!this.componentRef) { if (this.componentRef === null) {
this.initializeComponent(element); this.initializeComponent(element);
} }
} }
@ -97,15 +96,15 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
*/ */
disconnect() { disconnect() {
// Return if there is no componentRef or the component is already scheduled for destruction // Return if there is no componentRef or the component is already scheduled for destruction
if (!this.componentRef || this.scheduledDestroyFn !== null) { if (this.componentRef === null || this.scheduledDestroyFn !== null) {
return; return;
} }
// Schedule the component to be destroyed after a small timeout in case it is being // Schedule the component to be destroyed after a small timeout in case it is being
// moved elsewhere in the DOM // moved elsewhere in the DOM
this.scheduledDestroyFn = scheduler.schedule(() => { this.scheduledDestroyFn = scheduler.schedule(() => {
if (this.componentRef) { if (this.componentRef !== null) {
this.componentRef!.destroy(); this.componentRef.destroy();
this.componentRef = null; this.componentRef = null;
} }
}, DESTROY_DELAY); }, DESTROY_DELAY);
@ -116,11 +115,11 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
* retrieved from the cached initialization values. * retrieved from the cached initialization values.
*/ */
getInputValue(property: string): any { getInputValue(property: string): any {
if (!this.componentRef) { if (this.componentRef === null) {
return this.initialInputValues.get(property); return this.initialInputValues.get(property);
} }
return (this.componentRef.instance as any)[property]; return this.componentRef.instance[property];
} }
/** /**
@ -128,7 +127,7 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
* cached and set when the component is created. * cached and set when the component is created.
*/ */
setInputValue(property: string, value: any): void { setInputValue(property: string, value: any): void {
if (!this.componentRef) { if (this.componentRef === null) {
this.initialInputValues.set(property, value); this.initialInputValues.set(property, value);
return; return;
} }
@ -142,7 +141,7 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
} }
this.recordInputChange(property, value); this.recordInputChange(property, value);
(this.componentRef.instance as any)[property] = value; this.componentRef.instance[property] = value;
this.scheduleDetectChanges(); this.scheduleDetectChanges();
} }
@ -156,11 +155,10 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
extractProjectableNodes(element, this.componentFactory.ngContentSelectors); extractProjectableNodes(element, this.componentFactory.ngContentSelectors);
this.componentRef = this.componentFactory.create(childInjector, projectableNodes, element); this.componentRef = this.componentFactory.create(childInjector, projectableNodes, element);
this.implementsOnChanges = this.implementsOnChanges = isFunction((this.componentRef.instance as OnChanges).ngOnChanges);
isFunction((this.componentRef.instance as any as OnChanges).ngOnChanges);
this.initializeInputs(); this.initializeInputs();
this.initializeOutputs(); this.initializeOutputs(this.componentRef);
this.detectChanges(); this.detectChanges();
@ -188,17 +186,17 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
} }
/** Sets up listeners for the component's outputs so that the events stream emits the events. */ /** Sets up listeners for the component's outputs so that the events stream emits the events. */
protected initializeOutputs(): void { protected initializeOutputs(componentRef: ComponentRef<any>): void {
const eventEmitters = this.componentFactory.outputs.map(({propName, templateName}) => { const eventEmitters = this.componentFactory.outputs.map(({propName, templateName}) => {
const emitter = (this.componentRef!.instance as any)[propName] as EventEmitter<any>; const emitter: EventEmitter<any> = componentRef.instance[propName];
return emitter.pipe(map((value: any) => ({name: templateName, value}))); return emitter.pipe(map(value => ({name: templateName, value})));
}); });
this.events = merge(...eventEmitters); this.events = merge(...eventEmitters);
} }
/** Calls ngOnChanges with all the inputs that have changed since the last call. */ /** Calls ngOnChanges with all the inputs that have changed since the last call. */
protected callNgOnChanges(): void { protected callNgOnChanges(componentRef: ComponentRef<any>): void {
if (!this.implementsOnChanges || this.inputChanges === null) { if (!this.implementsOnChanges || this.inputChanges === null) {
return; return;
} }
@ -207,7 +205,7 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
// during ngOnChanges. // during ngOnChanges.
const inputChanges = this.inputChanges; const inputChanges = this.inputChanges;
this.inputChanges = null; this.inputChanges = null;
(this.componentRef!.instance as any as OnChanges).ngOnChanges(inputChanges); (componentRef.instance as OnChanges).ngOnChanges(inputChanges);
} }
/** /**
@ -230,7 +228,8 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
*/ */
protected recordInputChange(property: string, currentValue: any): void { protected recordInputChange(property: string, currentValue: any): void {
// Do not record the change if the component does not implement `OnChanges`. // Do not record the change if the component does not implement `OnChanges`.
if (this.componentRef && !this.implementsOnChanges) { // (We can only determine that after the component has been instantiated.)
if (this.componentRef !== null && !this.implementsOnChanges) {
return; return;
} }
@ -255,11 +254,11 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
/** Runs change detection on the component. */ /** Runs change detection on the component. */
protected detectChanges(): void { protected detectChanges(): void {
if (!this.componentRef) { if (this.componentRef === null) {
return; return;
} }
this.callNgOnChanges(); this.callNgOnChanges(this.componentRef);
this.componentRef!.changeDetectorRef.detectChanges(); this.componentRef.changeDetectorRef.detectChanges();
} }
} }