svelte: Property syntax using curly braces and quotes is inconsistent.

Describe the bug

There is this special syntax prop="{value}" that does not cause string interpolation but instead just passes the value of the expression as is. This is inconsistent with how properties and string interpolation behave in all other cases.

As I see it, as soon as quotes are used, the value should be an interpolated string, no magic. Obviously, this would be a significant breaking change, so this is more of a suggestion for the future.

Reproduction

<Child value="42" /> <!-- string -->
<Child value={42} /> <!-- number -->
<Child value="{42}" /> <!-- (magically) number -->
<Child value="{42} " /> <!-- string -->

REPL

Logs

No response

System Info

REPL (Svelte Version 3.51.0)
Windows 10, 64bit
Chrome 106.0.5249.103

Severity

annoyance

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 26
  • Comments: 25 (19 by maintainers)

Commits related to this issue

Most upvoted comments

Here’s what I think we should do: in dev mode, print a warning if a component prop or element on[x] attribute is quoted and the value is a non-string. Something like this:

The foo="{bar}" prop will be coerced to a string in Svelte 6. If this isn’t what you want, remove the quote marks

(Challenge: indicating where in the source code the offending prop was.)

If we do that, we can fix this weirdo behaviour in Svelte 6.

The warning is in place for components and custom elements now, moving this to 6.0 so we think about the next steps for that major when it comes to that.

This dates back a long time ago (I think Svelte 1?) where people would generally use HTML syntax highlighting when editing Svelte components, so we encouraged people to use foo="{bar}" rather than foo={bar} so that their file would be highlighted correctly. I wouldn’t be against changing this in Svelte 4.

It’s not added there because for onclick and bind:files there’s only one possible kind of value, so it doesn’t matter, whereas for attributes stringified or not makes a difference. But, we’ll possible fade these out over time, too (warning, then at some point maybe even error) - depends on how good the other transitions works out.

How is that a counter argument? This is just string interpolation in exactly the way I expect it to work. (Same as in element text content.)

Revisiting this, I still don’t think we should change this behavior. It’s arguably an edge case and AFAIK some people still use the prettier plugin’s HTML strict mode due to editor limitations which means quotes are always applied. I’m in favor of kicking this can down the road further.

Either add some types or configure your prettier/linter to format or yell at you.

I’m sorry, forget my comment, didn’t read thoroughly enough. Note to self: this would also need adjustment in prettier-plugin-svelte

Recently a warningFilter compiler option was added which could help:

compilerOptions: {
	warningFilter: ({ code }) => code != 'attribute_quoted',
},

Though the preprocessor probably needs to be adjusted if it currently always outputs the quotes 🤔

A counter argument to changing this is that you can do this today: class="{foo} some string"