Skip to content

Commit 922d982

Browse files
authored
refactor(material/autocomplete): declare injected properties as nullable (#26009)
In autocomplete-trigger.ts, add `| null` to the typing of optionally injected properties. This improves code health because the compiler can detect possible type errors with `null`.
1 parent 18664ed commit 922d982

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,13 @@ export abstract class _MatAutocompleteTriggerBase
201201
private _zone: NgZone,
202202
private _changeDetectorRef: ChangeDetectorRef,
203203
@Inject(MAT_AUTOCOMPLETE_SCROLL_STRATEGY) scrollStrategy: any,
204-
@Optional() private _dir: Directionality,
205-
@Optional() @Inject(MAT_FORM_FIELD) @Host() private _formField: MatFormField,
204+
@Optional() private _dir: Directionality | null,
205+
@Optional() @Inject(MAT_FORM_FIELD) @Host() private _formField: MatFormField | null,
206206
@Optional() @Inject(DOCUMENT) private _document: any,
207207
private _viewportRuler: ViewportRuler,
208208
@Optional()
209209
@Inject(MAT_AUTOCOMPLETE_DEFAULT_OPTIONS)
210-
private _defaults?: MatAutocompleteDefaultOptions,
210+
private _defaults?: MatAutocompleteDefaultOptions | null,
211211
) {
212212
this._scrollStrategy = scrollStrategy;
213213
}
@@ -506,7 +506,9 @@ export abstract class _MatAutocompleteTriggerBase
506506
/** If the label has been manually elevated, return it to its normal state. */
507507
private _resetLabel(): void {
508508
if (this._manuallyFloatingLabel) {
509-
this._formField.floatLabel = 'auto';
509+
if (this._formField) {
510+
this._formField.floatLabel = 'auto';
511+
}
510512
this._manuallyFloatingLabel = false;
511513
}
512514
}
@@ -680,7 +682,7 @@ export abstract class _MatAutocompleteTriggerBase
680682
positionStrategy: this._getOverlayPosition(),
681683
scrollStrategy: this._scrollStrategy(),
682684
width: this._getPanelWidth(),
683-
direction: this._dir,
685+
direction: this._dir ?? undefined,
684686
panelClass: this._defaults?.overlayPanelClass,
685687
});
686688
}

tools/public_api_guard/material/autocomplete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export class MatAutocompleteTrigger extends _MatAutocompleteTriggerBase {
193193

194194
// @public
195195
export abstract class _MatAutocompleteTriggerBase implements ControlValueAccessor, AfterViewInit, OnChanges, OnDestroy {
196-
constructor(_element: ElementRef<HTMLInputElement>, _overlay: Overlay, _viewContainerRef: ViewContainerRef, _zone: NgZone, _changeDetectorRef: ChangeDetectorRef, scrollStrategy: any, _dir: Directionality, _formField: MatFormField, _document: any, _viewportRuler: ViewportRuler, _defaults?: MatAutocompleteDefaultOptions | undefined);
196+
constructor(_element: ElementRef<HTMLInputElement>, _overlay: Overlay, _viewContainerRef: ViewContainerRef, _zone: NgZone, _changeDetectorRef: ChangeDetectorRef, scrollStrategy: any, _dir: Directionality | null, _formField: MatFormField | null, _document: any, _viewportRuler: ViewportRuler, _defaults?: MatAutocompleteDefaultOptions | null | undefined);
197197
protected abstract _aboveClass: string;
198198
get activeOption(): _MatOptionBase | null;
199199
autocomplete: _MatAutocompleteBase;

0 commit comments

Comments
 (0)