{"record":{"id":"0f2ca31f48077946","repo":"solidjs/solid","slug":"expected-the-observer-to-be-an-object","errorCode":null,"errorMessage":"Expected the observer to be an object.","messagePattern":"Expected the observer to be an object\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/solid/src/reactive/observable.ts","lineNumber":50,"sourceCode":"      next?: (v: T) => void;\n      error?: (v: any) => void;\n      complete?: (v: boolean) => void;\n    };\n/**\n * Creates a simple observable from a signal's accessor to be used with the `from` operator of observable libraries like e.g. rxjs\n * ```typescript\n * import { from } from \"rxjs\";\n * const [s, set] = createSignal(0);\n * const obsv$ = from(observable(s));\n * obsv$.subscribe((v) => console.log(v));\n * ```\n * description https://docs.solidjs.com/reference/reactive-utilities/observable\n */\nexport function observable<T>(input: Accessor<T>): Observable<T> {\n  return {\n    subscribe(observer: ObservableObserver<T>) {\n      if (!(observer instanceof Object) || observer == null) {\n        throw new TypeError(\"Expected the observer to be an object.\");\n      }\n\n      const handler =\n        typeof observer === \"function\" ? observer : observer.next && observer.next.bind(observer);\n\n      if (!handler) {\n        return { unsubscribe() {} };\n      }\n\n      const dispose = createRoot(disposer => {\n        createEffect(() => {\n          const v = input();\n          untrack(() => handler(v));\n        });\n\n        return disposer;\n      });\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/solidjs/solid/blob/f47845f9cc16ecbb316aa6560c7161f45af9a3d8/packages/solid/src/reactive/observable.ts#L32-L68","documentation":"Thrown by observable().subscribe() when the argument is not an object. The observable interop wraps a Solid accessor in an RxJS-style Observable, and the spec requires the observer to be either a function or an object with a next method. Solid validates this up front and throws a TypeError for null, undefined, or primitives.","triggerScenarios":"Calling observable(accessor).subscribe(null), subscribe(42), subscribe('x'), or an object without a next method combined with a non-function value; also de-serializing or cloning an observer so it arrives as a primitive.","commonSituations":"Passing an observer variable that is conditionally undefined; mixing up RxJS subscriber APIs (passing a config object instead of observer); migrating code where a function observer was refactored into an optional object.","solutions":["Pass either a plain function or an object with a next method: subscribe({ next: v => ... })","Default the observer: subscribe(observer || { next: () => {} })","If interoperating with RxJS, pipe the Solid observable into RxJS's from() rather than hand-writing observers"],"exampleFix":"// before\nconst obs = observable(signal);\nobs.subscribe(maybeObserver); // maybeObserver is undefined\n\n// after\nconst obs = observable(signal);\nconst sub = obs.subscribe(\n  maybeObserver ?? { next: (v) => console.log(v) }\n);\nsub.unsubscribe();","handlingStrategy":"type-guard","validationCode":"function isObserver(v: unknown): v is ((v: T) => void) | { next: (v: T) => void } {\n  return typeof v === 'function' || (!!v && typeof (v as any).next === 'function');\n}\nif (!isObserver(observer)) throw new Error('observer must be function or { next }');\nobservable(accessor).subscribe(observer as any);","typeGuard":"const isObserver = (v: unknown): v is { next: (v: unknown) => void } | ((v: unknown) => void) =>\n  typeof v === 'function' ||\n  (v instanceof Object && typeof (v as { next?: unknown }).next === 'function');","tryCatchPattern":"try { obs.subscribe(observer); } catch (e) { if (e instanceof TypeError && /observer to be an object/.test(e.message)) fixObserverShape(); else throw e; }","preventionTips":["Always construct observers as a function or { next } object literal at the call site","Default optional observers: observer ?? (() => {})","Type the observer parameter in your own wrappers so TS rejects primitives"],"tags":["solid","observable","rxjs","typeerror","validation"],"backgroundTag":"invalid-observer-argument","analyzedSha":"f47845f9cc16ecbb316aa6560c7161f45af9a3d8","analyzedAt":"2026-08-27T05:39:24.283Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}