sveltejs/svelte

`bind:value={[getter, setter]}` in a tuple to allow programatic creation of binding functions

Closed this issue · 1 comments

Describe the problem

It's a simple matter, but i find myself wanting this often. I'll keep it brief.

Describe the proposed solution

Allowing bind: to accept a tuple of a [getter, setter]:

<script lang="ts">
	const unassignable = {
		inner: 'value'
	};

	const createBinding = <T extends Record<string, any>>(object: T) => {
		return [() => object, (v: T) => doSomethingToObject(object, v)] as const;
	};
</script>

<Component bind:value={createBinding(unassignable)} />

<!-- OR / AND -->

<Component bind:value={...createBinding(unassignable)} />

Importance

nice to have

The tuple needs to be statically analyzable but furthermore you can just create an object with a get value and set value and bind to obj.value.