Type safe dialog afterClosed()

See original GitHub issue

Bug, feature request, or proposal:

feature request

What is the expected behavior?

MatDialogRef.afterClosed() return Observable of a type other than any.

What is the current behavior?

MatDialogRef.afterClosed() returns Observable<any>.

What is the use-case or motivation for changing an existing behavior?

type safety

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

all

Is there anything else we should know?

I tried:

export interface DialogComponent<R> { }

export interface TypedMatDialogRef<T extends DialogComponent<R>, R> extends MatDialogRef<T> {
  afterClosed(): Observable<R>;
}

export interface TypedMatDialog extends MatDialog {
  open<T extends DialogComponent<R>, R, D = any>(componentOrTemplateRef: ComponentType<T>, config?: MatDialogConfig<D>): TypedMatDialogRef<T, R>;
}

@Component({})
export class MyDialogComponent implements DialogComponent<string> { }

(this.dialog as TypedMatDialog).open(MyDialogComponent)
  .afterClosed()
  .subscribe(string => {/* string is showing as {}*/});

Not sure why this doesn’t work.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
andrewseguincommented, Jan 30, 2018

Here’s a quick example of defining the result type: https://stackblitz.com/edit/angular-iw4975?file=app/dialog-overview-example.ts

Note that result is typed as string via let dialogRef: MatDialogRef<DialogOverviewExampleDialog, string>

If you try changing it to number, you’ll see an error show up when you try to assign result to animal which demonstrates that it is typing correctly.

2reactions
william-lohancommented, Dec 14, 2017

Will there be an example of how to utilize?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to subscribe to angularMaterial Dialog afterClosed?
No need to unsubscribe from afterClosed() as it auto completes itself: ...
Read more >
Angular Material Dialog: A Complete Example
In this post, we are going to go through a complete example of how to build a custom dialog using the Angular Material...
Read more >
Dialog | Angular Material
The MatDialog service can be used to open modal dialogs with Material Design styling and ... afterClosed().subscribe(result => { console.log(`Dialog result: ...
Read more >
How can I unit test dialogRef.afterClosed() using Jest? - Reddit
My test configuration file is : TestBed.configureTestingModule({ declarations: [OptionTableComponent, WhoComponent, PropertiesComponent], ...
Read more >
mat-dialog-close not working - You.com | The AI Search ...
<button type="submit" (click)="addUser()" mat-dialog-close ... After inserting data on clicking save the HTTP POST service gets called and data gets ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found