Skip to content

Commit 571ef46

Browse files
crisbetoandrewseguin
authored andcommitted
fix(dialog): improved type safety in dialog ref result (#8766)
Adds an extra generic param to the `MatDialogRef` that allows consumers to type the result that is passed to `close`, as well as the value in the `beforeClosed` and `afterClosed` observables. Fixes #8760.
1 parent 9335224 commit 571ef46

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/lib/dialog/dialog-ref.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ let uniqueId = 0;
2323
/**
2424
* Reference to a dialog opened via the MatDialog service.
2525
*/
26-
export class MatDialogRef<T> {
26+
export class MatDialogRef<T, R = any> {
2727
/** The instance of component opened into the dialog. */
2828
componentInstance: T;
2929

@@ -34,13 +34,13 @@ export class MatDialogRef<T> {
3434
private _afterOpen = new Subject<void>();
3535

3636
/** Subject for notifying the user that the dialog has finished closing. */
37-
private _afterClosed = new Subject<any>();
37+
private _afterClosed = new Subject<R | undefined>();
3838

3939
/** Subject for notifying the user that the dialog has started closing. */
40-
private _beforeClose = new Subject<any>();
40+
private _beforeClose = new Subject<R | undefined>();
4141

4242
/** Result to be passed to afterClosed. */
43-
private _result: any;
43+
private _result: R | undefined;
4444

4545
constructor(
4646
private _overlayRef: OverlayRef,
@@ -74,7 +74,7 @@ export class MatDialogRef<T> {
7474
* Close the dialog.
7575
* @param dialogResult Optional result to return to the dialog opener.
7676
*/
77-
close(dialogResult?: any): void {
77+
close(dialogResult?: R): void {
7878
this._result = dialogResult;
7979

8080
// Transition the backdrop in parallel to the dialog.
@@ -101,14 +101,14 @@ export class MatDialogRef<T> {
101101
/**
102102
* Gets an observable that is notified when the dialog is finished closing.
103103
*/
104-
afterClosed(): Observable<any> {
104+
afterClosed(): Observable<R | undefined> {
105105
return this._afterClosed.asObservable();
106106
}
107107

108108
/**
109109
* Gets an observable that is notified when the dialog has started closing.
110110
*/
111-
beforeClose(): Observable<any> {
111+
beforeClose(): Observable<R | undefined> {
112112
return this._beforeClose.asObservable();
113113
}
114114

0 commit comments

Comments
 (0)