Closed
Description
Version
3.2.26
Reproduction link
Steps to reproduce
import { toRefs } from 'vue'
const props = defineProps<{
isLive?: boolean
}>()
const { isLive } = toRefs(props)
What is expected?
According to fix(runtime-core): ensure declare prop keys are always present type of isLive
should be:
Ref<boolean>
What is actually happening?
The type of isLive
actually is:
Ref<boolean | undefined> | undefined
isLive
couldn't be undefined in no case
Activity
liulinboyi commentedon May 2, 2022
In this test case if the props type is Boolean, absent should cast to false.
StephenChips commentedon May 3, 2022
Interesting, I tend to think this is a bug, since other primitive type's default value is
undefined
.liulinboyi commentedon May 3, 2022
If it's a bug,it's easy to fix,change the handling of this situation
to
but in this test case if the props type is Boolean, absent should cast to false.
StephenChips commentedon May 4, 2022
@liulinboyi
If is a not a bug, why we need this behaviour, what is the motivation of this design? It is kind of odd and inconsistent to other types. IMO, absent value should always be
undefined
.I noticed that only boolean props will do the casting, other types will just remain
undefined
(e.g. number, string).extrem7 commentedon May 4, 2022
@StephenChips I think there are two different moments:
Ref<boolean | undefined>
andRef<> | undefined
. Seems like the second has no sense (because keys are always present in props). The first case may be consistent behavior.ig1na commentedon Jun 14, 2022
Even when using withDefaults setting the prop to undefined, the type becomes
Ref<boolean>
but should beRef<boolean | undefined>
Is there any workaround to use watchers on props without using toRefs ?
Guebbit commentedon Aug 24, 2022
The problem is still there, I thought it was a little ugly to put an additional check, hope it will be fixed soon
defineProps
to set optional boolean prop #7116fix(types): optional boolean props should have boolean type in return…
fix(types): optional boolean props should have boolean type in return…
fix(types): optional boolean props should have boolean type in return…