Skip to content

Commit 0d3333b

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 25ce323 commit 0d3333b

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,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: MatOption,
42+
public source: MatOption<T>,
4343
/** Whether the change in the option's value was a result of a user action. */
4444
public isUserInput = false) { }
4545
}
@@ -63,7 +63,7 @@ export const MAT_OPTION_PARENT_COMPONENT =
6363

6464
@Directive()
6565
// tslint:disable-next-line:class-name
66-
export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDestroy {
66+
export class _MatOptionBase<T = any> implements FocusableOption, AfterViewChecked, OnDestroy {
6767
private _selected = false;
6868
private _active = false;
6969
private _disabled = false;
@@ -76,7 +76,7 @@ export class _MatOptionBase implements FocusableOption, AfterViewChecked, OnDest
7676
get selected(): boolean { return this._selected; }
7777

7878
/** The form value of the option. */
79-
@Input() value: any;
79+
@Input() value: T;
8080

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

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

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

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

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

tools/public_api_guard/material/core.d.ts

Lines changed: 11 additions & 11 deletions
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 {
@@ -236,10 +236,10 @@ export declare class MatOptgroup extends _MatOptgroupBase {
236236
static ɵfac: i0.ɵɵFactoryDef<MatOptgroup, never>;
237237
}
238238

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

245245
export declare class MatOptionModule {
@@ -252,11 +252,11 @@ export interface MatOptionParentComponent {
252252
multiple?: boolean;
253253
}
254254

255-
export declare class MatOptionSelectionChange {
255+
export declare class MatOptionSelectionChange<T = any> {
256256
isUserInput: boolean;
257-
source: MatOption;
257+
source: MatOption<T>;
258258
constructor(
259-
source: MatOption,
259+
source: MatOption<T>,
260260
isUserInput?: boolean);
261261
}
262262

0 commit comments

Comments
 (0)