-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Allow $refs to be undefined in Typescript types #11322
Comments
The flexibility is better because depending on the case, the ref migh always be present and checking for it to be undefined would be unnecessary |
@posva I understand the argument for making it opt-in, but I would like to stress the fact that currently it's not possible at all. This does not compile, because the current types don't allow you to specify
|
You should open a bug report with a boiled down reproduction at https://github.com/vuejs/vue-class-component then |
Thanks for the pointer, I didn't file there as that repo is not included in your issue helper options. The progress on this ticket can be followed under vuejs/vue-class-component#415. |
@posva Upon further inspection, the types stem from this repository, not the class component definition. They're defined here: https://github.com/vuejs/vue/blob/dev/types/vue.d.ts#L29 Should this ticket be reopened or is there a better course of action I can take to address this? |
What problem does this feature solve?
Currently, when you add
$refs
annotations to a class-based component, the types are defined as follows:This is handy and allows you to use refs as expected in class methods:
However, the types incorrectly assume that the ref is always present, while it might not be so.
In the above situation, the ref would be
undefined
if the component unmounted during the async request.What does the proposed API look like?
The correct types would ideally mark refs as possibly undefined automatically, or alternatively allow the user to do so.
Currently types don't allow this.
The above types would ensure the user correctly checks for the ref before calling a method on it:
The text was updated successfully, but these errors were encountered: