Skip to content

Don't use forced type conversion on bind, it can cause some unsolicited type conversion. #5574

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
Mlocik97 opened this issue Oct 23, 2020 · 1 comment

Comments

@Mlocik97
Copy link
Contributor

Mlocik97 commented Oct 23, 2020

Describe the bug
Let's say we use variable with type string, on use bind from input type range or number on that value. It's forced to retype to number, as You can see in Repl, but somethimes, You don't want to automatically force retype of variable if it's value is changed by bind from input type number or range, that is now forced, see

// <input type='range|number' bind:value>
if (type === 'range' || type === 'number') {
return x`@to_number(this.${name})`;
}

Logs
(none)

To Reproduce
https://svelte.dev/repl/64ae5620f0bd44adaba952ccfd444493?version=3.29.4

  • If you can demonstrate the bug using https://svelte.dev/repl, please do.
  • If that's not possible, we recommend creating a small repo that illustrates the problem.
  • Reproductions should be small, self-contained, correct examples – http://sscce.org.

Occasionally, this won't be possible, and that's fine – we still appreciate you raising the issue. But please understand that Svelte is run by unpaid volunteers in their free time, and issues that follow these instructions will get fixed faster.

Expected behavior
Have ability to change variable from input type number or range with preserve type string in variable

Information about your Svelte project:

  • Your browser and the version: Chrome and Firefox (newest verrsions)

  • Your operating system: Ubuntu 20.04

  • Svelte version: 3.29.4

  • Rollup

Severity
Medium, You need now to do:

let a = "5" // and use it to bind inputs.
$: a_string = String(a); // and use this in other code, but still there is problem, if a_string is changed from somewhere else.
// if You add another line:
$: a = Number(a_string); // but it will redeclare "a" so You need to use "a_string | 5", but it still not work:
// it will result in `Cyclical dependency detected: a_string → a → a_string (3:1)`
// so there is not easy solution for this.

Additional context
We should propably don't depend on type of input, but on another attribute, that will say, what type we will really want in result, like

<input bindtype="string"> // or any other type

propably for other binds as well

@Mlocik97 Mlocik97 changed the title Don't use forced type conversion on bind, it can cause some mismatched type conversion. Don't use forced type conversion on bind, it can cause some unsolicited type conversion. Oct 23, 2020
@Conduitry
Copy link
Member

Essentially a duplicate of #3937.

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

2 participants