Skip to content

Commit 1a721e5

Browse files
authored
fix: only throw bind error when not passing a value (#10090)
Relax the runtime error to only throw when you're passing a binding with an undefined value. This makes it possible to provide components in a way that can be used more flexibly while keeping the error to guard against the case that we want to avoid: a default value propagation up.
1 parent 9161448 commit 1a721e5

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

.changeset/big-geese-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: only throw bind error when not passing a value

packages/svelte/src/internal/client/runtime.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,16 +1567,20 @@ export function is_store(val) {
15671567
export function prop(props, key, flags, initial) {
15681568
var immutable = (flags & PROPS_IS_IMMUTABLE) !== 0;
15691569
var runes = (flags & PROPS_IS_RUNES) !== 0;
1570-
1571-
var setter = get_descriptor(props, key)?.set;
1572-
if (DEV && setter && runes && initial !== undefined) {
1573-
// TODO consolidate all these random runtime errors
1574-
throw new Error('Cannot use fallback values with bind:');
1575-
}
1576-
15771570
var prop_value = /** @type {V} */ (props[key]);
1571+
var setter = get_descriptor(props, key)?.set;
15781572

15791573
if (prop_value === undefined && initial !== undefined) {
1574+
if (setter && runes) {
1575+
// TODO consolidate all these random runtime errors
1576+
throw new Error(
1577+
'ERR_SVELTE_BINDING_FALLBACK' +
1578+
(DEV
1579+
? `: Cannot pass undefined to bind:${key} because the property contains a fallback value. Pass a different value than undefined to ${key}.`
1580+
: '')
1581+
);
1582+
}
1583+
15801584
// @ts-expect-error would need a cumbersome method overload to type this
15811585
if ((flags & PROPS_IS_LAZY_INITIAL) !== 0) initial = initial();
15821586

packages/svelte/tests/runtime-runes/samples/props-bound-fallback/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export default test({
1414
assert.htmlEqual(target.innerHTML, `<button>1</button><span>1</span>`);
1515
},
1616

17-
error: `Cannot use fallback values with bind:`
17+
error: `ERR_SVELTE_BINDING_FALLBACK: Cannot pass undefined to bind:count because the property contains a fallback value. Pass a different value than undefined to count.`
1818
});

0 commit comments

Comments
 (0)