Skip to content
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

v-if type guard does not effect events #1796

Closed
markbrockhoff opened this issue Aug 31, 2022 · 2 comments
Closed

v-if type guard does not effect events #1796

markbrockhoff opened this issue Aug 31, 2022 · 2 comments

Comments

@markbrockhoff
Copy link

markbrockhoff commented Aug 31, 2022

Hi,

I noticed that the "type guard" functionality given by v-if effects props and the template but not events.

Example:
image

In this example I have a ref of an object called selectedDepartment the whole object can be undefined. To eliminate this case I use a v-if as a type guard to check that it is defined before using it inside the template.
This works like a normal if in typescript for the prop and template, but not the event.
I'd expect Volar to reduce the type of selectedDepartment to not be undefined like in the prop:
image

@xiaoxiangmoe
Copy link
Collaborator

xiaoxiangmoe commented Aug 31, 2022

This is because callback function may be called at any time.
For example:

setTimeout(()=>{
   emit('click')
}, 100*1000);

This will emit click after 100s, but component may be destroyed at 50s.
So the type narrow in callback function is unsound.


But if you really want to use it, for the sake of completeness, see #1750 (comment)

@markbrockhoff
Copy link
Author

Ok, I see. Thanks for the quick answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants