sveltejs/svelte · error · Error

Object cannot be null

Error message

Object cannot be null

What it means

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'`.

Source

Thrown at packages/svelte/src/motion/tweened.js:38

	if (a === b || a !== a) return () => a;

	const type = typeof a;
	if (type !== typeof b || Array.isArray(a) !== Array.isArray(b)) {
		throw new Error('Cannot interpolate values of different type');
	}

	if (Array.isArray(a)) {
		const arr = /** @type {Array<any>} */ (b).map((bi, i) => {
			return get_interpolator(/** @type {Array<any>} */ (a)[i], bi);
		});

		// @ts-ignore
		return (t) => arr.map((fn) => fn(t));
	}

	if (type === 'object') {
		if (!a || !b) {
			throw new Error('Object cannot be null');
		}

		if (is_date(a) && is_date(b)) {
			const an = a.getTime();
			const bn = b.getTime();
			const delta = bn - an;

			// @ts-ignore
			return (t) => new Date(an + t * delta);
		}

		const keys = Object.keys(b);

		/** @type {Record<string, (t: number) => T>} */
		const interpolators = {};
		keys.forEach((key) => {
			// @ts-ignore
			interpolators[key] = get_interpolator(a[key], b[key]);

View on GitHub (pinned to 20b341f100)

Solutions

  1. Avoid null — use an empty object `{}` instead of `null` for 'no data'.
  2. Reset the store without interpolation: `.set({}, { duration: 0 })`.
  3. Guard nullable values: `.set(value ?? {})`.

Example fix

// before
const pos = tweened({ x: 0, y: 0 });
pos.set(null); // object -> null -> throws
// after
pos.set({ x: 0, y: 0 }); // keep object shape
// or snap-reset
pos.set({}, { duration: 0 });
Defensive patterns

Strategy: validation

Validate before calling

function ensureObject(v) {
	if (v !== null && typeof v === 'object' && !(v instanceof Date) && !Array.isArray(v)) return v;
	throw new Error('Tween object value cannot be null');
}
tweenStore.set(ensureObject(next));

Type guard

function isNonNullObject(v) {
	return v !== null && typeof v === 'object' && !(v instanceof Date) && !Array.isArray(v);
}

Prevention

When it happens

Trigger: 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`.

Common situations: 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`.

Related errors


AI-assisted analysis of sveltejs/svelte@20b341f100 (2026-08-12). Data as JSON: /api/errors/991df8e9ca8248aa. Report an issue: GitHub.