Skip to content

prop did't produce right value #9041

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

Closed
yangjunjun opened this issue Nov 8, 2018 · 4 comments
Closed

prop did't produce right value #9041

yangjunjun opened this issue Nov 8, 2018 · 4 comments

Comments

@yangjunjun
Copy link

Version

2.5.17

Reproduction link

https://jsfiddle.net/yangjunjun/eywraw8t/450799/

Steps to reproduce

see the demo, it's simple

What is expected?

the first prop 'val' should be empty string

What is actually happening?

the first prop 'val' is true


when I create a checkbox-like component which has a props "value" who accept Boolean String type. when an empty string was assign to the "value" but the actual value is "true", it's weird.

@Justineo
Copy link
Member

Justineo commented Nov 8, 2018

As you declared the prop as type [Boolean, String], Vue would try to figure out it can be treated as a boolean value first. And for boolean props, as long as it exists it's regarded as truthy.

<!-- the following declarations are equivalent -->
<input disabled>
<input disabled="">
<input disabled="whatever">

You can declare the type as [String, Boolean] if you want to regard the value as string first.

@Justineo Justineo closed this as completed Nov 8, 2018
@yangjunjun
Copy link
Author

yangjunjun commented Nov 8, 2018

It doesn't make sense. the order of props type should not affect the result. the props accept String and I assign string, it should be a string. Now it's confusing.
Anyway the official docs didn't mention this.

@Justineo
Copy link
Member

Justineo commented Nov 8, 2018

And this implementation (depending on the order of the type array) is intentional:

// only cast empty string / same name to boolean if
// boolean has higher priority
const stringIndex = getTypeIndex(String, prop.type)
if (stringIndex < 0 || booleanIndex < stringIndex) {
value = true
}

If you feel this is still confusing, welcome to file an issue or make a pull request to our documentation repo: vuejs/vuejs.org. Thanks.

@KaelWD
Copy link
Contributor

KaelWD commented Nov 9, 2018

See #7485 and the linked issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants