{"record":{"id":"ab02e124d829268f","repo":"ReactiveX/rxjs","slug":"observable-constructor-requires-a-callback","errorCode":null,"errorMessage":"Observable constructor requires a callback","messagePattern":"Observable constructor requires a callback","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/observable-polyfill/src/index.ts","lineNumber":586,"sourceCode":"          (output) => {\n            subscriber.next(output);\n            subscriber.complete();\n          },\n          (error) => (subscriber as Subscriber<T>)[errorSubscriber](error, false)\n        );\n      });\n    }\n\n    throw new TypeError(`${String(value)} is not observable`);\n  }\n\n  #subscriber: WeakRef<Subscriber<T>> | null = null;\n\n  readonly #init: (subscriber: Subscriber<T>) => void;\n\n  constructor(init: (subscriber: Subscriber<T>) => void) {\n    if (typeof init !== 'function') {\n      throw new TypeError('Observable constructor requires a callback');\n    }\n    this.#init = init;\n  }\n\n  subscribe(observer: Partial<Observer<T>> | ((value: T) => void) | null = {}, options: SubscribeOptions = {}): void {\n    if (!canInvokeRealmCallbacks()) {\n      return;\n    }\n\n    let subscriber = this.#subscriber?.deref();\n\n    const shouldSubscribe = !subscriber?.active;\n\n    if (shouldSubscribe) {\n      subscriber = new Subscriber(subscriberToken);\n      this.#subscriber = new WeakRef(subscriber);\n    }\n","sourceCodeStart":568,"sourceCodeEnd":604,"githubUrl":"https://github.com/ReactiveX/rxjs/blob/54796b38a57e6309f9861e174737479bb3f63f61/packages/observable-polyfill/src/index.ts#L568-L604","documentation":"The Observable constructor (ObservableImpl) requires a subscriber callback function that receives a Subscriber and sets up the observable's behavior. Passing anything else — undefined, a string, an object, null — throws this TypeError immediately at construction time, matching the platform Observable specification.","triggerScenarios":"new Observable(), new Observable(null), new Observable('handler'), new Observable({ next(...) {} }), or passing a variable that is undefined due to a bad import/typo: new Observable(subscrib).","commonSituations":"Typos or wrong names in the callback argument; passing an Observer object (next/error/complete) instead of an initializer function — a habit from subscribe(); refactoring where the function was moved and the import broke; default-parameter refactor leaving the argument undefined.","solutions":["Pass a function: new Observable(subscriber => { subscriber.next(value); subscriber.complete(); }).","If you meant to pass an observer, that belongs in subscribe(), not the constructor.","Check the argument is not undefined (broken import or renamed symbol).","Consider a factory helper to centralize construction and validate inputs."],"exampleFix":"// before\nnew Observable({ next: v => console.log(v) });\n// after\nnew Observable(subscriber => { subscriber.next(1); subscriber.complete(); });","handlingStrategy":"type-guard","validationCode":"if (typeof init === 'function') { new Observable(init); } else { new Observable(() => {}); }","typeGuard":"function isSubscriberCallback(v: unknown): v is (subscriber: any) => void {\n  return typeof v === 'function';\n}","tryCatchPattern":"try { const o = new Observable(init); } catch (e) { if (e instanceof TypeError && /requires a callback/.test(e.message)) { /* supply correct initializer function */ } else throw e; }","preventionTips":["Pass a function, never an Observer object, to the constructor.","Double-check argument names/imports before constructing.","Keep Observer objects for subscribe() only."],"tags":["observable-constructor","typeerror","callback-required","api-misuse"],"backgroundTag":"constructor-requires-callback","analyzedSha":"54796b38a57e6309f9861e174737479bb3f63f61","analyzedAt":"2026-08-28T10:21:27.410Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}