angular / angular/components

[MatDialog]: Infer return-type `R` of `MatDialogRef<T, R>` based on component passed into `MatDialog.open()`

Open
#24,538 15 comments 51 reactions 0 assignees View on GitHub
area: material/dialog feature P3
Dominant language
TypeScript
Stars
25k
Forks
6.8k
Avg merge
1d 8h
Merged PRs (30d)
91

Description

### Feature Description

If I call:

```
MatDialog
.open(ADialogComponent)
.afterClosed()
.subscribe((result) => {
// result should have a type rather than `any`
});
```

It would be nice if `result` could be typed based on what `ADialogComponent` is known to return. Perhaps some work would need to be done for a component to be able to specify its return type. Maybe it would need to implement an interface `MatDialogComponent` or something. I don't know the solution...I just know the current way leaves a lot of room for error.

As things are currently, the caller must specify the return type when calling `MatDialog.open()` like this:

```
MatDialog
.open(ADialogComponent)
.afterClosed()
.subscribe((result: ReturnTypeHere) => {
// result is typed as ReturnTypeHere because it was specified when calling MatDialog.open()
});
```

The gives the caller the opportunity to incorrectly specify the return type...or to not specify one at all.

### Use Case

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reading the MatDialog.open() and MatDialogRef typings described in the issue, then trace how the component passed to open() relates to the afterClosed() result. Done means the result type is inferred from the component without requiring callers to provide an explicit return-type generic, with the supported component contract documented or tested.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.