{"record":{"id":"991df8e9ca8248aa","repo":"sveltejs/svelte","slug":"object-cannot-be-null","errorCode":null,"errorMessage":"Object cannot be null","messagePattern":"Object cannot be null","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/motion/tweened.js","lineNumber":38,"sourceCode":"\tif (a === b || a !== a) return () => a;\n\n\tconst type = typeof a;\n\tif (type !== typeof b || Array.isArray(a) !== Array.isArray(b)) {\n\t\tthrow new Error('Cannot interpolate values of different type');\n\t}\n\n\tif (Array.isArray(a)) {\n\t\tconst arr = /** @type {Array<any>} */ (b).map((bi, i) => {\n\t\t\treturn get_interpolator(/** @type {Array<any>} */ (a)[i], bi);\n\t\t});\n\n\t\t// @ts-ignore\n\t\treturn (t) => arr.map((fn) => fn(t));\n\t}\n\n\tif (type === 'object') {\n\t\tif (!a || !b) {\n\t\t\tthrow new Error('Object cannot be null');\n\t\t}\n\n\t\tif (is_date(a) && is_date(b)) {\n\t\t\tconst an = a.getTime();\n\t\t\tconst bn = b.getTime();\n\t\t\tconst delta = bn - an;\n\n\t\t\t// @ts-ignore\n\t\t\treturn (t) => new Date(an + t * delta);\n\t\t}\n\n\t\tconst keys = Object.keys(b);\n\n\t\t/** @type {Record<string, (t: number) => T>} */\n\t\tconst interpolators = {};\n\t\tkeys.forEach((key) => {\n\t\t\t// @ts-ignore\n\t\t\tinterpolators[key] = get_interpolator(a[key], b[key]);","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/motion/tweened.js#L20-L56","documentation":"Inside `get_interpolator`, when `typeof a === 'object'` (which is also true for `null` in JavaScript), Svelte checks both `a` and `b` are truthy objects. If either is `null`, it throws — you cannot interpolate into or out of `null`. This is a special case of the type guard because `typeof null === 'object'`.","triggerScenarios":"Tweening from an object to `null` or from `null` to an object; calling `.set(null)` on a tweened store initialized with an object; an object-valued tween whose source or target resolves to `null`.","commonSituations":"Optional object state that becomes `null` while a tween is in flight; API data where a field is sometimes `null`; resetting a tweened object to 'empty' using `null`.","solutions":["Avoid null — use an empty object `{}` instead of `null` for 'no data'.","Reset the store without interpolation: `.set({}, { duration: 0 })`.","Guard nullable values: `.set(value ?? {})`."],"exampleFix":"// before\nconst pos = tweened({ x: 0, y: 0 });\npos.set(null); // object -> null -> throws\n// after\npos.set({ x: 0, y: 0 }); // keep object shape\n// or snap-reset\npos.set({}, { duration: 0 });","handlingStrategy":"validation","validationCode":"function ensureObject(v) {\n\tif (v !== null && typeof v === 'object' && !(v instanceof Date) && !Array.isArray(v)) return v;\n\tthrow new Error('Tween object value cannot be null');\n}\ntweenStore.set(ensureObject(next));","typeGuard":"function isNonNullObject(v) {\n\treturn v !== null && typeof v === 'object' && !(v instanceof Date) && !Array.isArray(v);\n}","tryCatchPattern":null,"preventionTips":["Use empty objects `{}` instead of `null` for tweened object state.","Reset tweens with `{ duration: 0 }` rather than `.set(null)`.","Coerce nullable inputs: `value ?? {}`.","Keep tweened object values non-null throughout their lifecycle."],"tags":["tweened","motion","null","interpolation","svelte-motion"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}