Skip to content

Commit 74099a0

Browse files
authored
fix(material/core): make MatOption generic (#20242)
Makes `MatOption` generic so that consumers can type its `value` to something different from `any`. Fixes #19456.
1 parent 1926b19 commit 74099a0

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

src/material-experimental/mdc-core/option/option.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {MatOptgroup} from './optgroup';
4848
encapsulation: ViewEncapsulation.None,
4949
changeDetection: ChangeDetectionStrategy.OnPush,
5050
})
51-
export class MatOption extends _MatOptionBase {
51+
export class MatOption<T = any> extends _MatOptionBase<T> {
5252
constructor(
5353
element: ElementRef<HTMLElement>,
5454
changeDetectorRef: ChangeDetectorRef,

src/material/core/option/option.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ import {MatOptionParentComponent, MAT_OPTION_PARENT_COMPONENT} from './option-pa
3636
let _uniqueIdCounter = 0;
3737

3838
/** Event object emitted by MatOption when selected or deselected. */
39-
export class MatOptionSelectionChange {
39+
export class MatOptionSelectionChange<T = any> {
4040
constructor(
4141
/** Reference to the option that emitted the event. */
42-
public source: _MatOptionBase,
42+
public source: _MatOptionBase<T>,
4343
/** Whether the change in the option's value was a result of a user action. */
4444
public isUserInput = false,
4545
) {}
4646
}
4747

4848
@Directive()
49-
export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDestroy {
49+
export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecked, OnDestroy {
5050
private _selected = false;
5151
private _active = false;
5252
private _disabled = false;
@@ -63,7 +63,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
6363
}
6464

6565
/** The form value of the option. */
66-
@Input() value: any;
66+
@Input() value: T;
6767

6868
/** The unique ID of the option. */
6969
@Input() id: string = `mat-option-${_uniqueIdCounter++}`;
@@ -84,7 +84,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
8484

8585
/** Event emitted when the option is selected or deselected. */
8686
// tslint:disable-next-line:no-output-on-prefix
87-
@Output() readonly onSelectionChange = new EventEmitter<MatOptionSelectionChange>();
87+
@Output() readonly onSelectionChange = new EventEmitter<MatOptionSelectionChange<T>>();
8888

8989
/** Emits when the state of the option changes and any parents have to be notified. */
9090
readonly _stateChanges = new Subject<void>();
@@ -237,7 +237,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
237237

238238
/** Emits the selection change event. */
239239
private _emitSelectionChangeEvent(isUserInput = false): void {
240-
this.onSelectionChange.emit(new MatOptionSelectionChange(this, isUserInput));
240+
this.onSelectionChange.emit(new MatOptionSelectionChange<T>(this, isUserInput));
241241
}
242242
}
243243

@@ -266,7 +266,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
266266
encapsulation: ViewEncapsulation.None,
267267
changeDetection: ChangeDetectionStrategy.OnPush,
268268
})
269-
export class MatOption extends _MatOptionBase {
269+
export class MatOption<T = any> extends _MatOptionBase<T> {
270270
constructor(
271271
element: ElementRef<HTMLElement>,
272272
changeDetectorRef: ChangeDetectorRef,

tools/public_api_guard/material/core.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,16 @@ export class _MatOptgroupBase extends _MatOptgroupMixinBase implements CanDisabl
266266
}
267267

268268
// @public
269-
export class MatOption extends _MatOptionBase {
269+
export class MatOption<T = any> extends _MatOptionBase<T> {
270270
constructor(element: ElementRef<HTMLElement>, changeDetectorRef: ChangeDetectorRef, parent: MatOptionParentComponent, group: MatOptgroup);
271271
// (undocumented)
272-
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption, "mat-option", ["matOption"], {}, {}, never, ["*"]>;
272+
static ɵcmp: i0.ɵɵComponentDeclaration<MatOption<any>, "mat-option", ["matOption"], {}, {}, never, ["*"]>;
273273
// (undocumented)
274-
static ɵfac: i0.ɵɵFactoryDeclaration<MatOption, [null, null, { optional: true; }, { optional: true; }]>;
274+
static ɵfac: i0.ɵɵFactoryDeclaration<MatOption<any>, [null, null, { optional: true; }, { optional: true; }]>;
275275
}
276276

277277
// @public (undocumented)
278-
export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDestroy {
278+
export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecked, OnDestroy {
279279
constructor(_element: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, _parent: MatOptionParentComponent, group: _MatOptgroupBase);
280280
get active(): boolean;
281281
deselect(): void;
@@ -296,19 +296,19 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
296296
ngAfterViewChecked(): void;
297297
// (undocumented)
298298
ngOnDestroy(): void;
299-
readonly onSelectionChange: EventEmitter<MatOptionSelectionChange>;
299+
readonly onSelectionChange: EventEmitter<MatOptionSelectionChange<T>>;
300300
select(): void;
301301
get selected(): boolean;
302302
_selectViaInteraction(): void;
303303
setActiveStyles(): void;
304304
setInactiveStyles(): void;
305305
readonly _stateChanges: Subject<void>;
306-
value: any;
306+
value: T;
307307
get viewValue(): string;
308308
// (undocumented)
309-
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatOptionBase, never, never, { "value": "value"; "id": "id"; "disabled": "disabled"; }, { "onSelectionChange": "onSelectionChange"; }, never>;
309+
static ɵdir: i0.ɵɵDirectiveDeclaration<_MatOptionBase<any>, never, never, { "value": "value"; "id": "id"; "disabled": "disabled"; }, { "onSelectionChange": "onSelectionChange"; }, never>;
310310
// (undocumented)
311-
static ɵfac: i0.ɵɵFactoryDeclaration<_MatOptionBase, never>;
311+
static ɵfac: i0.ɵɵFactoryDeclaration<_MatOptionBase<any>, never>;
312312
}
313313

314314
// @public (undocumented)
@@ -332,12 +332,12 @@ export interface MatOptionParentComponent {
332332
}
333333

334334
// @public
335-
export class MatOptionSelectionChange {
335+
export class MatOptionSelectionChange<T = any> {
336336
constructor(
337-
source: _MatOptionBase,
337+
source: _MatOptionBase<T>,
338338
isUserInput?: boolean);
339339
isUserInput: boolean;
340-
source: _MatOptionBase;
340+
source: _MatOptionBase<T>;
341341
}
342342

343343
// @public

0 commit comments

Comments
 (0)