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

Boolean v-model doesn't default to undefined when not set #9882

Closed
CernyMatej opened this issue Dec 20, 2023 · 4 comments
Closed

Boolean v-model doesn't default to undefined when not set #9882

CernyMatej opened this issue Dec 20, 2023 · 4 comments

Comments

@CernyMatej
Copy link

CernyMatej commented Dec 20, 2023

Vue version

3.3.13

Link to minimal reproduction

https://play.vuejs.org/#eNp9UstugzAQ/BXLF1opj0NuEYnUVDm0Uh9qevSFwIaSGtuyF5oK8e9dm0BTNeLE7O4wO7PQ8DtjZnUFfMljl9rCIJOJyleCoxOcOcDKrIVKtXLI3JPOQLIVy+BQKAhV7NAWKl/fRB2Ibnv2/hp7r7WERBH9jDw/nneraREVCKWRCQJVjMVZUQdA0BvxgLFdWMXqaRlWKE3eAJesaRh+G9CH3mrbsimLqD3U0Vls7tX+CW86U2PK51iD8lD/UfbPzno8vwjEJ3RXOs+hyGdHpxWdvfFkwVNdmkKCfTFY0PkEp52doOCJlPrrMfTQVjDp++kHpJ9X+kd38j3BXy04sDUIPswwsTlgN97unuFEeBhS6EoSe2T4Bk7LynvsaJtKZWT7ghfcPpRGW6SP9O62JwTl+lDeqGe2gS84/Xv3I9F/7S5mi/CeUC1vfwBRQuR4

Steps to reproduce

  1. Define a model with boolean type in a component
  2. Do not specify the v-model when using the component in the template of a parent component

What is expected?

Since all other types have undefined value when they're not set, I would expect a boolean v-model to behave the same.
I assume the root cause of this problem is that normal boolean props are converted to false when not set.

I can see why this behavior might be desired for props in certain situations. However, it creates issues in detecting whether a v-model has been set. Therefore, I think it would be better to have consistency in prop behavior.

What is actually happening?

A v-model of boolean type has a value of false when not set, instead of undefined.

System Info

No response

Any additional comments?

No response

@LinusBorg
Copy link
Member

This is documented behavior so 1) it's not a bug and 2) will not be changed in Vue 3.

The Boolean absent props will be cast to false. You can change this by setting a default for it — i.e.: default: undefined to behave as a non-Boolean prop.

@CernyMatej
Copy link
Author

I know it's documented but I think the use-case for boolean props is different than boolean v-model. Wouldn't you at least consider adding the default value of undefined to the defineModel() macro so that it is consistent with other types of v-model?

@LinusBorg
Copy link
Member

You can define a default in defineModel() just like you can for the normal Boolean prop. I don't think it's desirable to switch the behavior for defineModel<boolean>() only, that will only cause more confusion.

@matthew-dean
Copy link

@CernyMatej This has been a very-long-standing error in Vue, but it's probably too risky to correct it now.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants