{"record":{"id":"4d69078ac9f163ea","repo":"sveltejs/svelte","slug":"derived-expects-stores-as-input-got-a-falsy-val","errorCode":null,"errorMessage":"derived() expects stores as input, got a falsy value","messagePattern":"derived\\(\\) expects stores as input, got a falsy value","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/store/shared/index.js","lineNumber":134,"sourceCode":" * @param {S} stores\n * @param {(values: StoresValues<S>) => T} fn\n * @param {T} [initial_value]\n * @returns {Readable<T>}\n */\n/**\n * @template {Stores} S\n * @template T\n * @param {S} stores\n * @param {Function} fn\n * @param {T} [initial_value]\n * @returns {Readable<T>}\n */\nexport function derived(stores, fn, initial_value) {\n\tconst single = !Array.isArray(stores);\n\t/** @type {Array<Readable<any>>} */\n\tconst stores_array = single ? [stores] : stores;\n\tif (!stores_array.every(Boolean)) {\n\t\tthrow new Error('derived() expects stores as input, got a falsy value');\n\t}\n\tconst auto = fn.length < 2;\n\treturn readable(initial_value, (set, update) => {\n\t\tlet started = false;\n\t\t/** @type {T[]} */\n\t\tconst values = [];\n\t\tlet pending = 0;\n\t\tlet cleanup = noop;\n\t\tconst sync = () => {\n\t\t\tif (pending) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tcleanup();\n\t\t\tconst result = fn(single ? values[0] : values, set, update);\n\t\t\tif (auto) {\n\t\t\t\tset(result);\n\t\t\t} else {\n\t\t\t\tcleanup = typeof result === 'function' ? result : noop;","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/store/shared/index.js#L116-L152","documentation":"`derived(stores, fn)` synthesizes one store from others. It validates that every store in the array (or the single store) is truthy — not `null`/`undefined`/`0`/`''`/`false`. A falsy entry usually means a store was never initialized or an import failed. The guard runs before subscribing, so it fails fast.","triggerScenarios":"Passing an array with a falsy element: `derived([a, b], ...)` where `b` is `undefined`; passing a single falsy store: `derived(undefined, ...)`; a store variable that is conditionally defined or came from a failed import.","commonSituations":"Dynamic store arrays with an undefined member; refactoring that leaves a store unset; SSR/import issues yielding undefined; an array built from optional values without filtering.","solutions":["Ensure every store in the array is a valid store (defined, with `subscribe`).","Filter falsy members before passing: `stores.filter(Boolean)`.","Provide defaults: `[a, b ?? readable(null)]`."],"exampleFix":"// before\nconst d = derived([storeA, storeB], ([a, b]) => a + b); // storeB is undefined -> throws\n// after\nimport { readable } from 'svelte/store';\nconst d = derived([storeA, storeB ?? readable(0)], ([a, b]) => a + b);","handlingStrategy":"validation","validationCode":"function assertStores(stores) {\n\tconst arr = Array.isArray(stores) ? stores : [stores];\n\tif (!arr.every((s) => s && typeof s.subscribe === 'function')) {\n\t\tthrow new Error('derived() requires all inputs to be valid stores');\n\t}\n}\nassertStores([a, b]);\nconst d = derived([a, b], fn);","typeGuard":"function areAllStores(v) {\n\tconst arr = Array.isArray(v) ? v : [v];\n\treturn arr.every((s) => s != null && typeof s.subscribe === 'function');\n}","tryCatchPattern":null,"preventionTips":["Filter falsy members before passing arrays to `derived`: `stores.filter(Boolean)`.","Provide defaults: `store ?? readable(initial)`.","Check store imports resolve (not undefined) at module load.","Validate store arrays in dev before subscribing."],"tags":["store","derived","validation","svelte-store"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}