svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Two way bind with question mark

Open sciefylab opened this issue 1 year ago • 2 comments

Describe the problem

I have input field with nested object properties, but sometimes the properties was null, i don't want to add variables to passing it.

Describe the proposed solution

Please allow,

https://svelte.dev/repl/5db9ddbb548042ad97398f47fb1a6ae0?version=3.59.1

Alternatives considered

i didn't know

Importance

would make my life easier

sciefylab avatar May 30 '23 00:05 sciefylab

In the shared link the second record has location as null.

I guess the ? doesn't work in Svelte. Hence it can be made an empty object, like so: location: {}

As empty object:

https://svelte.dev/repl/8b62518f09464dfd986f726d9324c7fa?version=3.59.1

I hope this helps.

srikantgudi avatar May 30 '23 02:05 srikantgudi

temporary i using this to convert null to object,

https://stackoverflow.com/questions/38078630/javascript-null-to-empty-object-properties

thanks

sciefylab avatar Jun 03 '23 03:06 sciefylab

This isn't possible for the same reason that item.location?.name = value isn't possible in JavaScript — you can't assign a property to a non-existent object, and you can't just create item.location in order to assign that property. This would lead to ambiguity (what should happen when the input is updated?) and confusion.

Rich-Harris avatar Apr 03 '24 14:04 Rich-Harris