inconsisten behaviour of missing required boolean prop
Vue version
3.2.45
Link to minimal reproduction
Steps to reproduce
Create component with required boolean and string prop, use this component without passing any prop
What is expected?
Either boolean prop should be passed as undefined
or it shouldn't be throwing missing prop
warning
Additionally foo?: boolean
prop type is boolean | undefined
, while with the object declaration (foo: Boolean
, no required: true
) it's just boolean
as expected
What is actually happening?
boolean prop is passed as false
string prop is passed as undefined
System Info
No response
Any additional comments?
If it's not a bug then at least it's confusing and bad DX in my opinion
Expected and documented behavior. Missing Boolean props are cast to false
every since Vue 2 at least. I get the confusion this may cause, but that API is set like this until we talk about breaking changes for Vue 4 some time in the future.
https://vuejs.org/guide/components/props.html#boolean-casting
I get though, that we could improve this docs section as it can be misunderstood (and has a few typos in the types in the composition API example).
Expected and documented behavior
Could you please point me to the docs section saying that the type of ?: boolean
prop (which as I understand would be equivalent of { type: Boolean }
in object syntax) is boolean | undefined
although the value is never undefined
(even when we pass undefined
explicitely) and that the TS behaviour is different for options API and composition API?
- The fact that the TS behavior is different when using
defineProps()
is the bug discussed in #7487. The inferred type should just beboolean
, because missing boolean props are always cast tofalse
, so they can't beundefined
. - The behavior for
{ type: Boolean }
is explained in the section I linked to (switch docs to options API). - The composition API version of that same section seems to have a typo in that the
?:
operator is missing in the example. I'm on the process of opening an issue about that and also suggest some re-wording.
Edit: I closed this issues here as the "expected behavior" was worded as "should pass undefined
" which it should not, so there's nothing to fix regarding that.
I kinda missed that you also described the types problem reported in #7487. Can we continue discussion about that bug in that new issue, as it doesn't have two topics intertwined?