Skip to content

Commit ea66ce2

Browse files
committed
fix(core): make MatOption generic
Makes `MatOption` generic so that consumers can type its `value` to something different from `any`. Fixes #19456.
1 parent 4ef3d3f commit ea66ce2

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

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

+1-1
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

+7-7
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ import {MatOptgroup, _MatOptgroupBase, MAT_OPTGROUP} from './optgroup';
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
}
@@ -62,7 +62,7 @@ export const MAT_OPTION_PARENT_COMPONENT =
6262

6363

6464
@Directive()
65-
export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDestroy {
65+
export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecked, OnDestroy {
6666
private _selected = false;
6767
private _active = false;
6868
private _disabled = false;
@@ -75,7 +75,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
7575
get selected(): boolean { return this._selected; }
7676

7777
/** The form value of the option. */
78-
@Input() value: any;
78+
@Input() value: T;
7979

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

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

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

243243
/** Emits the selection change event. */
244244
private _emitSelectionChangeEvent(isUserInput = false): void {
245-
this.onSelectionChange.emit(new MatOptionSelectionChange(this, isUserInput));
245+
this.onSelectionChange.emit(new MatOptionSelectionChange<T>(this, isUserInput));
246246
}
247247

248248
static ngAcceptInputType_disabled: BooleanInput;
@@ -273,7 +273,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
273273
encapsulation: ViewEncapsulation.None,
274274
changeDetection: ChangeDetectionStrategy.OnPush,
275275
})
276-
export class MatOption extends _MatOptionBase {
276+
export class MatOption<T = any> extends _MatOptionBase<T> {
277277
constructor(
278278
element: ElementRef<HTMLElement>,
279279
changeDetectorRef: ChangeDetectorRef,

tools/public_api_guard/material/core.d.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export declare class _MatOptgroupBase extends _MatOptgroupMixinBase implements C
1010
static ɵfac: i0.ɵɵFactoryDef<_MatOptgroupBase, never>;
1111
}
1212

13-
export declare class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDestroy {
13+
export declare class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecked, OnDestroy {
1414
readonly _stateChanges: Subject<void>;
1515
get active(): boolean;
1616
get disableRipple(): boolean | undefined;
@@ -19,9 +19,9 @@ export declare class _MatOptionBase implements FocusableOption, AfterViewChecked
1919
readonly group: _MatOptgroupBase;
2020
id: string;
2121
get multiple(): boolean | undefined;
22-
readonly onSelectionChange: EventEmitter<MatOptionSelectionChange>;
22+
readonly onSelectionChange: EventEmitter<MatOptionSelectionChange<T>>;
2323
get selected(): boolean;
24-
value: any;
24+
value: T;
2525
get viewValue(): string;
2626
constructor(_element: ElementRef<HTMLElement>, _changeDetectorRef: ChangeDetectorRef, _parent: MatOptionParentComponent, group: _MatOptgroupBase);
2727
_getAriaSelected(): boolean | null;
@@ -38,8 +38,8 @@ export declare class _MatOptionBase implements FocusableOption, AfterViewChecked
3838
setActiveStyles(): void;
3939
setInactiveStyles(): void;
4040
static ngAcceptInputType_disabled: BooleanInput;
41-
static ɵdir: i0.ɵɵDirectiveDefWithMeta<_MatOptionBase, never, never, { "value": "value"; "id": "id"; "disabled": "disabled"; }, { "onSelectionChange": "onSelectionChange"; }, never>;
42-
static ɵfac: i0.ɵɵFactoryDef<_MatOptionBase, never>;
41+
static ɵdir: i0.ɵɵDirectiveDefWithMeta<_MatOptionBase<any>, never, never, { "value": "value"; "id": "id"; "disabled": "disabled"; }, { "onSelectionChange": "onSelectionChange"; }, never>;
42+
static ɵfac: i0.ɵɵFactoryDef<_MatOptionBase<any>, never>;
4343
}
4444

4545
export declare class AnimationCurves {
@@ -222,10 +222,10 @@ export declare class MatOptgroup extends _MatOptgroupBase {
222222
static ɵfac: i0.ɵɵFactoryDef<MatOptgroup, never>;
223223
}
224224

225-
export declare class MatOption extends _MatOptionBase {
225+
export declare class MatOption<T = any> extends _MatOptionBase<T> {
226226
constructor(element: ElementRef<HTMLElement>, changeDetectorRef: ChangeDetectorRef, parent: MatOptionParentComponent, group: MatOptgroup);
227-
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatOption, "mat-option", ["matOption"], {}, {}, never, ["*"]>;
228-
static ɵfac: i0.ɵɵFactoryDef<MatOption, [null, null, { optional: true; }, { optional: true; }]>;
227+
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatOption<any>, "mat-option", ["matOption"], {}, {}, never, ["*"]>;
228+
static ɵfac: i0.ɵɵFactoryDef<MatOption<any>, [null, null, { optional: true; }, { optional: true; }]>;
229229
}
230230

231231
export declare class MatOptionModule {
@@ -238,11 +238,11 @@ export interface MatOptionParentComponent {
238238
multiple?: boolean;
239239
}
240240

241-
export declare class MatOptionSelectionChange {
241+
export declare class MatOptionSelectionChange<T = any> {
242242
isUserInput: boolean;
243-
source: _MatOptionBase;
243+
source: _MatOptionBase<T>;
244244
constructor(
245-
source: _MatOptionBase,
245+
source: _MatOptionBase<T>,
246246
isUserInput?: boolean);
247247
}
248248

0 commit comments

Comments
 (0)