From 569d1ef583fc46107d0f9c3f8bcfb2dff7b00c00 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Fri, 5 Jun 2020 20:14:16 +0300 Subject: [PATCH] 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 --- .../src/component-factory-strategy.ts | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/packages/elements/src/component-factory-strategy.ts b/packages/elements/src/component-factory-strategy.ts index ce542c6aac..3e5fcc5212 100644 --- a/packages/elements/src/component-factory-strategy.ts +++ b/packages/elements/src/component-factory-strategy.ts @@ -48,8 +48,7 @@ export class ComponentNgElementStrategy implements NgElementStrategy { events!: Observable; /** Reference to the component that was created on connect. */ - // TODO(issue/24571): remove '!'. - private componentRef!: ComponentRef|null; + private componentRef: ComponentRef|null = null; /** Changes that have been made to the component ref since the last time onChanges was called. */ private inputChanges: SimpleChanges|null = null; @@ -86,7 +85,7 @@ export class ComponentNgElementStrategy implements NgElementStrategy { return; } - if (!this.componentRef) { + if (this.componentRef === null) { this.initializeComponent(element); } } @@ -97,15 +96,15 @@ export class ComponentNgElementStrategy implements NgElementStrategy { */ disconnect() { // 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; } // Schedule the component to be destroyed after a small timeout in case it is being // moved elsewhere in the DOM this.scheduledDestroyFn = scheduler.schedule(() => { - if (this.componentRef) { - this.componentRef!.destroy(); + if (this.componentRef !== null) { + this.componentRef.destroy(); this.componentRef = null; } }, DESTROY_DELAY); @@ -116,11 +115,11 @@ export class ComponentNgElementStrategy implements NgElementStrategy { * retrieved from the cached initialization values. */ getInputValue(property: string): any { - if (!this.componentRef) { + if (this.componentRef === null) { 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. */ setInputValue(property: string, value: any): void { - if (!this.componentRef) { + if (this.componentRef === null) { this.initialInputValues.set(property, value); return; } @@ -142,7 +141,7 @@ export class ComponentNgElementStrategy implements NgElementStrategy { } this.recordInputChange(property, value); - (this.componentRef.instance as any)[property] = value; + this.componentRef.instance[property] = value; this.scheduleDetectChanges(); } @@ -156,11 +155,10 @@ export class ComponentNgElementStrategy implements NgElementStrategy { extractProjectableNodes(element, this.componentFactory.ngContentSelectors); this.componentRef = this.componentFactory.create(childInjector, projectableNodes, element); - this.implementsOnChanges = - isFunction((this.componentRef.instance as any as OnChanges).ngOnChanges); + this.implementsOnChanges = isFunction((this.componentRef.instance as OnChanges).ngOnChanges); this.initializeInputs(); - this.initializeOutputs(); + this.initializeOutputs(this.componentRef); 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. */ - protected initializeOutputs(): void { + protected initializeOutputs(componentRef: ComponentRef): void { const eventEmitters = this.componentFactory.outputs.map(({propName, templateName}) => { - const emitter = (this.componentRef!.instance as any)[propName] as EventEmitter; - return emitter.pipe(map((value: any) => ({name: templateName, value}))); + const emitter: EventEmitter = componentRef.instance[propName]; + return emitter.pipe(map(value => ({name: templateName, value}))); }); this.events = merge(...eventEmitters); } /** Calls ngOnChanges with all the inputs that have changed since the last call. */ - protected callNgOnChanges(): void { + protected callNgOnChanges(componentRef: ComponentRef): void { if (!this.implementsOnChanges || this.inputChanges === null) { return; } @@ -207,7 +205,7 @@ export class ComponentNgElementStrategy implements NgElementStrategy { // during ngOnChanges. const inputChanges = this.inputChanges; 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 { // 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; } @@ -255,11 +254,11 @@ export class ComponentNgElementStrategy implements NgElementStrategy { /** Runs change detection on the component. */ protected detectChanges(): void { - if (!this.componentRef) { + if (this.componentRef === null) { return; } - this.callNgOnChanges(); - this.componentRef!.changeDetectorRef.detectChanges(); + this.callNgOnChanges(this.componentRef); + this.componentRef.changeDetectorRef.detectChanges(); } }