SFC ts mode, the boolean of defineProps will convert the empty string to true
Vue version
3.2.37
Link to minimal reproduction
Steps to reproduce
F12 open devtool
What is expected?
Printed true
What is actually happening?
Should print the empty string
System Info
No response
Any additional comments?
No response
I found out that this is a known thing, is there a description in the documentation please? Because this can cause runtime errors.
This mirrors HTML behavior. Both of these will disable the input
<input type="text" disabled>
<input type="text" disabled="">
Thank you for your reply.
But is this definition necessarily related to the HTML behavior you are talking about, I just want to get the correct props in the script.
I'm guessing that the value in the props is not the expected value because the correct attribute needs to be set for the element in dom.
I have an idea that I don't know if it works. By distinguishing between component nodes and normal element nodes to get the correct props.
<input disabled=""></input>
<Comp disabled="" />
I find explanation in vue3 document as below:
The Boolean absent props will be cast to false. You should set a default value for it in order to get desired behavior. https://vuejs.org/guide/components/props.html#prop-validation
so as long as you set the Boolean props(except definitely false), it will cast to true
I have a doubt. Why do you define a Boolean type, but you pass a string
const props = defineProps<{ a: string | boolean //It has to do with the order in which you place it }>() //If you're not sure if the variable is a string or a Boolean type. You can set it up this way console.log(typeof props.a) // string
This is expected and has been discussed many times. If you want to allow it as an empty string as well Please, search existing issues and pull requests before opening issues, it saves maintainers a lot of time 🙏
@GDGHD-123 Do you think it's a good solution when it's the user who takes the initiative to solve a non-intuitive problem?