-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Svelte typecasting where it shouldn't #657
Comments
Interesting. In one sense it does sound good for However, I'm worried about that causing more confusion than it's worth. The only time something like this would come up I think is when you have literal values in the template. (If you had What I'd suggest here for passing in the actual string |
Oh, I just noticed the part about edit: My feeling is that the only ways to make this nicer would have to be considered breaking changes. I don't feel like there's a bug here really, just some undocumented nuances. The way it's implemented now, only things that look like numbers (i.e., |
I'm not sure how to handle this. Do we convert "true"/"false" to |
This is entirely my bad — I thought it would be convenient to coerce numbers but didn't think about Since there's a relatively easy workaround I think we're probably okay leaving the current behaviour for now, and thinking about what we'd ideally like to happen for v2. I think the least surprising thing would be if everything was passed down as strings. Since it's kind of ugly to write <Component value={false}/>
<!-- maybe still treat quoted single tags as expressions,
for the sake of syntax highlighters etc -->
<Component value={foo || bar}/> <!-- colours are weird -->
<Component value='{foo || bar}'/> <!-- colours are fine --> Realise that's a separate discussion (for which we should probably have a separate issue), but interested in immediate reactions. |
I like the idea of using a different syntax when you want to pass in anything other than a static string. It takes care of this problem, and (depending on the syntax that's settled upon) is a little easier to type. I don't know that I'd worry too much about how syntax current coloring treats the syntax we use. What I'm a little worried about is whether a syntax that looks like JSX but isn't would be much less upsetting than syntax that looks like mustache but isn't. |
Something else worthwhile might be to rethink what the tags for if/each/etc look like. Replacing |
|
@Rich-Harris Both Vue and Moon have workarounds for this that I think can be applied to Svelte. All properties as passed as strings, but when using a special syntax ( <Component value="someString"/> <!-- passes a string "someString" -->
<Component literal:value="item"/> <!-- passes the value of "item" -->
<Component literal:value="true"/> <!-- passes the boolean true -->
<Component literal:value="0"/> <!-- passes the number 0 --> Basically, items in the |
@kingpixil In a way it is the opposite of Svelte's: literal string attributes become the special case. It is elegant because the attribute value is simply always a JavaScript expression. |
This is debatable. True for data bound values but often data binding is overkill. Many components use configuration that is rather static.
|
True, although there's nothing wrong with One thing that's a lot nicer with Svelte's syntax currently is interpolation in attributes: |
I don't know if this is related, but one issue that annoys me is how Svelte handles undefined. Let's say I want a CustomButton that looks like this:
but when I use the CustomButton without an id, it passes
which I think is unexpected behavior.. is this a different issue or related? |
@PaulBGD That is native DOM behaviour:
This will also result in a |
Yes but it feels bad with binding, because there's no way to pass a value that represents no value (null maybe?) |
Second @PaulBGD, this is unnecessarily bloating the output. I don't see a realistic use case where you actually want the attribute value to be the string |
Fixed, finally, in v2.0.0. Thanks! |
Svelte is incorrectly casting numeric strings to numbers when assigned to component properties. This results in strange truthy behavior:
"1" is truthy
"true" is truthy
"false" is truthy
"0" is falsey
"
0
" is falseyAll of these strings should be truthy, but the number (and html entity for 0) are falsey.
https://svelte.technology/repl?version=1.22.5&gist=b75e1acf7ee81e6fb2b6d0ae0e8349d5
The text was updated successfully, but these errors were encountered: