Returning "" instead null when use v-model.number
Vue version
3.2.47
Link to minimal reproduction
https://codepen.io/leo-bnu/pen/rNqGqWJ
Steps to reproduce
- Access https://codepen.io/leo-bnu/pen/rNqGqWJ
- Clear the field
- Check the returned value
What is expected?
The returned value should be null
.
What is actually happening?
The returned value is ""
.
System Info
No response
Any additional comments?
No response
Hi @LinusBorg
Retuning a number as string, is not a good practice and may cause some side effects:
- Changing type at runtime is not a good practice (and breaks the Typescript type previously defined)
- If you have to calculate, all calculated values will be string, it's like a virus
- If you have to send back the value to an API, it may reject because string is not a number
- Add a lot of code to convert the value from string to number
Why use null instead?
- No type change at runtime, seems a good practice
- If you have to calculate, all calculated values will be number
- If you send back the value to API, it will work
- Save a lot of code, you do not need to convert the string to number anymore
- It has a better DX
Explaining better the (2) point:
1 + "" + 1 = "11" // bad
1 + 1 + "" = "2" // bad
1 + null + 1 = 2 // good
1 + 1 + null = 2 // good
In my opinion there are no reason to return "". I will understand if this is a breaking change. At a first moment I can't see what will break with this change.
I just fell into this bug as well.
I expected null
. The ""
makes the value a string. This means at runtime the variable is effectively number | string
instead of number
or number | null
.
Also, the <input type="number">
in vanilla JS has .value
and .valueAsNumber
that returns ""
and null
on empty fields.
Would be nice to have the same behavior here.
Also, the
<input type="number">
in vanilla JS has.value
and.valueAsNumber
that returns""
andnull
on empty fields. Would be nice to have the same behavior here.
Amending my comment that I'm also seeing this in <input type="number">
.
Hi, I made a codepen in vanilla JS and returns null (tested in Chrome).
https://codepen.io/leo-bnu/pen/KKGrXxv