Components: Type safe dialog afterClosed()

Created on 1 Dec 2017  路  5Comments  路  Source: angular/components

Bug, feature request, or proposal:

feature request

What is the expected behavior?

MatDialogRef.afterClosed() return Observable of a type other thanany.

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.

P3 has pr

Most helpful comment

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.

All 5 comments

Will there be an example of how to utilize?

@crisbeto @andrewseguin To add to @gatimus I don't see any examples of this in the docs. Could you provide a quick sample here and consider adding something to the docs?

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.

i would hardly call that a type safe dialog
when it automates the form and the template based on ANY interface automagically without having to key in every named field then it will be type safe

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MurhafSousli picture MurhafSousli  路  3Comments

dzrust picture dzrust  路  3Comments

crutchcorn picture crutchcorn  路  3Comments

michaelb-01 picture michaelb-01  路  3Comments

theunreal picture theunreal  路  3Comments