-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MatDialog]: Infer return-type R
of MatDialogRef<T, R>
based on component passed into MatDialog.open()
#24538
Comments
R
of MatDialogRef<T, R> based on component passed into MatDialog.open()R
of MatDialogRef<T, R>
based on component passed into MatDialog.open()
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list. You can find more details about the feature request process in our documentation. |
Based on my investigation with this in another class, I don't think TypeScript supports that sort of inferred generic. However we've got some experts on the team who might be able to help |
It should be possible using something like this:
Then the open call type becomes:
|
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends. Find more details about Angular's feature request process in our documentation. |
MatDialog is really cumbersome. Not only is the return type unsafe, but it's also very annoying that as soon as you want to specify return type, you have to specify Too bad it didn't get enough votes. I hope it's reconsidered. MatDialog/CdkDialog needs a typesafe option in some way. |
ideally Neither having input or output typesafe is unfortunate |
I also don't know how this should be resolved but would definitely be an enormous improvement regarding type safety. Maybe someday this could be reconsidered? While I haven't tested it out yet, an interesting (yet, somewhat cumbersome) way to mitigate the missing link between the type of the result value and the parent component is detailed in this blog post by @robinpellegrims. It involves creating an abstract class that your child component can extend from and a custom dialog service as follows:
|
@carlos-ds That's terrific, that's the workaround I was looking for for monthes! It's working very well, thanks so much! |
@Frankitch no worries but all credits go to the original author, I'm just the messenger. Edit: in the meantime, I also had the chance to play around with the solution I referenced above and it's a really good solution indeed if you feel type safety is of utmost importance. The only necessary change was replacing |
@carlos-ds thanks for sharing ❤️
@Frankitch how would that look in the code? (cc #12898 (comment)) |
@aceArt-GmbH, my abstract class just looks like this:
And now you do not have to pass arguments when you call |
Resolved: `No provider for InjectionToken` errorhm, with either solution, I seem to get
and similar (Angular 15 using legacy material) Works with @Frankitch @carlos-ds do you not have this problem with lazyLoaded modules? works with |
Feature Description
If I call:
It would be nice if
result
could be typed based on whatADialogComponent
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 interfaceMatDialogComponent<T, R>
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<T, R>()
like this:The gives the caller the opportunity to incorrectly specify the return type...or to not specify one at all.
Use Case
No response
The text was updated successfully, but these errors were encountered: