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.
pull/10468/head
Simon H 11 months ago committed by GitHub
parent 9161448ee5
commit 1a721e5916
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: only throw bind error when not passing a value

@ -1567,16 +1567,20 @@ export function is_store(val) {
export function prop(props, key, flags, initial) { export function prop(props, key, flags, initial) {
var immutable = (flags & PROPS_IS_IMMUTABLE) !== 0; var immutable = (flags & PROPS_IS_IMMUTABLE) !== 0;
var runes = (flags & PROPS_IS_RUNES) !== 0; var runes = (flags & PROPS_IS_RUNES) !== 0;
var prop_value = /** @type {V} */ (props[key]);
var setter = get_descriptor(props, key)?.set; var setter = get_descriptor(props, key)?.set;
if (DEV && setter && runes && initial !== undefined) {
if (prop_value === undefined && initial !== undefined) {
if (setter && runes) {
// TODO consolidate all these random runtime errors // TODO consolidate all these random runtime errors
throw new Error('Cannot use fallback values with bind:'); 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}.`
: '')
);
} }
var prop_value = /** @type {V} */ (props[key]);
if (prop_value === undefined && initial !== undefined) {
// @ts-expect-error would need a cumbersome method overload to type this // @ts-expect-error would need a cumbersome method overload to type this
if ((flags & PROPS_IS_LAZY_INITIAL) !== 0) initial = initial(); if ((flags & PROPS_IS_LAZY_INITIAL) !== 0) initial = initial();

@ -14,5 +14,5 @@ export default test({
assert.htmlEqual(target.innerHTML, `<button>1</button><span>1</span>`); assert.htmlEqual(target.innerHTML, `<button>1</button><span>1</span>`);
}, },
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.`
}); });

Loading…
Cancel
Save