From 1a721e5916bb71639f8fcc2cdffe3e99988ff578 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Mon, 12 Feb 2024 19:31:10 +0100 Subject: [PATCH] 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. --- .changeset/big-geese-act.md | 5 +++++ packages/svelte/src/internal/client/runtime.js | 18 +++++++++++------- .../samples/props-bound-fallback/_config.js | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 .changeset/big-geese-act.md diff --git a/.changeset/big-geese-act.md b/.changeset/big-geese-act.md new file mode 100644 index 0000000000..08e1e29d15 --- /dev/null +++ b/.changeset/big-geese-act.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: only throw bind error when not passing a value diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 21abccf38e..1fe72efa41 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -1567,16 +1567,20 @@ export function is_store(val) { export function prop(props, key, flags, initial) { var immutable = (flags & PROPS_IS_IMMUTABLE) !== 0; var runes = (flags & PROPS_IS_RUNES) !== 0; - - var setter = get_descriptor(props, key)?.set; - if (DEV && setter && runes && initial !== undefined) { - // TODO consolidate all these random runtime errors - throw new Error('Cannot use fallback values with bind:'); - } - var prop_value = /** @type {V} */ (props[key]); + var setter = get_descriptor(props, key)?.set; if (prop_value === undefined && initial !== undefined) { + if (setter && runes) { + // TODO consolidate all these random runtime errors + throw new Error( + 'ERR_SVELTE_BINDING_FALLBACK' + + (DEV + ? `: Cannot pass undefined to bind:${key} because the property contains a fallback value. Pass a different value than undefined to ${key}.` + : '') + ); + } + // @ts-expect-error would need a cumbersome method overload to type this if ((flags & PROPS_IS_LAZY_INITIAL) !== 0) initial = initial(); diff --git a/packages/svelte/tests/runtime-runes/samples/props-bound-fallback/_config.js b/packages/svelte/tests/runtime-runes/samples/props-bound-fallback/_config.js index 6dc700cee5..f382120e88 100644 --- a/packages/svelte/tests/runtime-runes/samples/props-bound-fallback/_config.js +++ b/packages/svelte/tests/runtime-runes/samples/props-bound-fallback/_config.js @@ -14,5 +14,5 @@ export default test({ assert.htmlEqual(target.innerHTML, `1`); }, - error: `Cannot use fallback values with bind:` + 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.` });