solidjs/solid · error · Error
Unexpected type ${typeof unwrappedStore} received when initi
Error message
Unexpected type ${typeof unwrappedStore} received when initializing 'createStore'. Expected an object. What it means
createStore requires initial state that unwraps to an object or function (including arrays). Passing a primitive throws this dev-only error because the store proxy needs a node to wrap; the message includes the actual typeof received to pinpoint the mismatch. For primitive reactive state, createSignal is the intended API.
Source
Thrown at packages/solid/store/src/store.ts:541
): void;
}
export type StoreReturn<T> = [get: Store<T>, set: SetStoreFunction<T>];
/**
* Creates a reactive store that can be read through a proxy object and written with a setter function
*
* @description https://docs.solidjs.com/reference/store-utilities/create-store
*/
export function createStore<T extends object = {}>(
...[store, options]: {} extends T
? [store?: T | Store<T>, options?: { name?: string }]
: [store: T | Store<T>, options?: { name?: string }]
): StoreReturn<T> {
const unwrappedStore = unwrap((store || {}) as T);
const isArray = Array.isArray(unwrappedStore);
if (IS_DEV && typeof unwrappedStore !== "object" && typeof unwrappedStore !== "function")
throw new Error(
`Unexpected type ${typeof unwrappedStore} received when initializing 'createStore'. Expected an object.`
);
const wrappedStore = wrap(unwrappedStore);
if (IS_DEV) DEV!.registerGraph({ value: unwrappedStore, name: options && options.name });
function setStore(...args: any[]): void {
batch(() => {
isArray && args.length === 1
? updateArray(unwrappedStore, args[0])
: updatePath(unwrappedStore, args);
});
}
return [wrappedStore, setStore];
}
View on GitHub (pinned to f47845f9cc)
Solutions
- Switch primitive state to createSignal: const [v, setV] = createSignal(0)
- Box the value in the store: createStore({ value: 0 })
- Validate/normalize initial data before createStore: assert typeof data === 'object'
Example fix
// before
const [count, setCount] = createStore(0); // throws: typeof 'number'
// after
const [count, setCount] = createSignal(0);
// or
const [state, setState] = createStore({ count: 0 }); Defensive patterns
Strategy: type-guard
Validate before calling
const isStoreState = (v: unknown): v is object | Function =>
typeof v === 'object' || typeof v === 'function';
if (!isStoreState(initial)) throw new TypeError('createStore requires object/array initial state');
const [state, setState] = createStore(initial); Type guard
const isStoreState = (v: unknown): v is Record<PropertyKey, unknown> | unknown[] => !!v && typeof v === 'object';
Prevention
- Reserve createStore for objects/arrays; use createSignal for scalars
- Assert typeof on data from APIs/parsers before storing
- Type store initializers so primitives fail at compile time
When it happens
Trigger: createStore('text'), createStore(0), createStore(true); passing unwrapped JSON like JSON.parse('"hi"'); spreading props or forwarding a field that is sometimes a primitive; defaulting with (store || {}) failing when store is a non-null primitive.
Common situations: Migrating useState/useState-like primitive state to stores during React ports; REST payloads whose root can be a scalar; type errors where Store<T> vs T confusion lets a primitive through.
Related errors
- Unexpected type ${typeof unwrappedStore} received when initi
- Expected the observer to be an object.
- Dispose method must be an explicit argument to createRoot fu
- Cannot mutate a Store directly
- Refusing to set "__proto__" on a store.
AI-assisted analysis of solidjs/solid@f47845f9cc (2026-08-27).
Data as JSON: /api/errors/ec29fff3fd374b49.
Report an issue: GitHub.