Subscribe on changes!

SFC ts mode, the boolean of defineProps will convert the empty string to true

avatar
Oct 14th 2022

I found out that this is a known thing, is there a description in the documentation please? Because this can cause runtime errors.

https://github.com/vuejs/core/blob/82a73da351bb5d26735f734ae2540a3033c00c9e/packages/runtime-core/__tests__/componentProps.spec.ts#L150

avatar
Oct 14th 2022

This mirrors HTML behavior. Both of these will disable the input

  <input type="text" disabled>
  <input type="text" disabled="">

https://jsfiddle.net/gnafbzru/

avatar
Oct 14th 2022

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="" />
avatar
Oct 14th 2022

I have a doubt. Why do you define a Boolean type, but you pass a string

avatar
Oct 14th 2022

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

avatar
Oct 14th 2022

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

avatar
Oct 14th 2022

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 🙏

avatar
Oct 14th 2022

@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?

avatar
Oct 14th 2022

@smallnine9 thx

avatar
Oct 14th 2022

@Linkontoask because <component a> equals to <component a="">