diff --git a/CHANGELOG.md b/CHANGELOG.md index d1e76c1705..3ad9a97b43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,9 +10,11 @@ * **breaking** Stricter types for `onMount` - now throws a type error when returning a function asynchronously to catch potential mistakes around callback functions (see PR for migration instructions) ([#8136](https://github.com/sveltejs/svelte/pull/8136)) * **breaking** Overhaul and drastically improve creating custom elements with Svelte (see PR for list of changes and migration instructions) ([#8457](https://github.com/sveltejs/svelte/pull/8457)) * **breaking** Deprecate `SvelteComponentTyped`, use `SvelteComponent` instead ([#8512](https://github.com/sveltejs/svelte/pull/8512)) +* **breaking** Error on falsy values instead of stores passed to `derived` ([#7947](https://github.com/sveltejs/svelte/pull/7947)) * Add `a11y no-noninteractive-element-interactions` rule ([#8391](https://github.com/sveltejs/svelte/pull/8391)) * Add `a11y-no-static-element-interactions`rule ([#8251](https://github.com/sveltejs/svelte/pull/8251)) * Bind `null` option and input values consistently ([#8312](https://github.com/sveltejs/svelte/issues/8312)) +* Allow `$store` to be used with changing values including nullish values ([#7555](https://github.com/sveltejs/svelte/issues/7555)) ## Unreleased (3.0) diff --git a/src/runtime/store/index.ts b/src/runtime/store/index.ts index 13131d4cc0..9a4ad40da0 100644 --- a/src/runtime/store/index.ts +++ b/src/runtime/store/index.ts @@ -164,10 +164,10 @@ export function derived( export function derived(stores: Stores, fn: Function, initial_value?: T): Readable { const single = !Array.isArray(stores); - const stores_array: Array> = single - // Fall back to readable() for falsy stores in array, else derived store will be forever pending - ? [(stores || readable()) as Readable] - : (stores as Array>).map(store => store || readable()); + const stores_array: Array> = single ? [stores as Readable] : stores as Array>; + if (!stores_array.every(Boolean)) { + throw new Error('derived() expects stores as input, got a falsy value') + } const auto = fn.length < 2; diff --git a/test/store/index.js b/test/store/index.js index d9c806a486..7487903ed4 100644 --- a/test/store/index.js +++ b/test/store/index.js @@ -429,37 +429,19 @@ describe('store', () => { assert.equal(b_started, false); }); - it('works with undefined stores #1', () => { - const a = derived(null, (n) => { - return n; + it('errors on undefined stores #1', () => { + assert.throws(() => { + derived(null, (n) => n); }); - const values = []; - const unsubscribe = a.subscribe((value) => values.push(value)); - unsubscribe(); - assert.deepEqual(values, [undefined]); }); - it('works with undefined stores #2', () => { - const a = writable(1); - const b = derived([a, null, undefined], ([n, un1, un2]) => { - assert.equal(un1, undefined); - assert.equal(un2, undefined); - return n * 2; - }); - - const values = []; - - const unsubscribe = b.subscribe(value => { - values.push(value); + it('errors on undefined stores #2', () => { + assert.throws(() => { + const a = writable(1); + derived([a, null, undefined], ([n]) => { + return n * 2; + }); }); - - a.set(2); - assert.deepEqual(values, [2, 4]); - - unsubscribe(); - - a.set(3); - assert.deepEqual(values, [2, 4]); }); });