{"record":{"id":"a56519da507b7fd8","repo":"ReactiveX/rxjs","slug":"refcount-requires-a-connectableobservable","errorCode":null,"errorMessage":"refCount requires a ConnectableObservable","messagePattern":"refCount requires a ConnectableObservable","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/rxjs/src/ref-count.ts","lineNumber":23,"sourceCode":"export const refCount: unique symbol = Symbol('refCount');\n\ndeclare global {\n  interface Observable<T> {\n    [refCount](this: ConnectableObservable<T>): Observable<T>;\n  }\n}\n\ninterface RefCountState {\n  activeRuns: number;\n  connecting: boolean;\n  connection: ConnectableConnection | null;\n}\n\nconst states = new WeakMap<object, RefCountState>();\n\nObservable.prototype[refCount] = function <T>(this: ConnectableObservable<T>): Observable<T> {\n  if (!(this instanceof ConnectableObservable)) {\n    throw new TypeError('refCount requires a ConnectableObservable');\n  }\n\n  const source = this;\n\n  return Observable[create]<T>((subscriber) => {\n    const state = getState(source);\n    state.activeRuns++;\n    let counted = true;\n\n    subscriber.addTeardown(() => {\n      if (!counted) {\n        return;\n      }\n      counted = false;\n\n      state.activeRuns--;\n      if (state.activeRuns !== 0) {\n        return;","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/ReactiveX/rxjs/blob/54796b38a57e6309f9861e174737479bb3f63f61/packages/rxjs/src/ref-count.ts#L5-L41","documentation":"The refCount Symbol operator only operates on a ConnectableObservable (the result of publish/publishReplay/multicast). Calling it on a plain Observable throws a TypeError, because refCounting requires the connectable's underlying connection state.","triggerScenarios":"obs.pipe(shareReplay({refCount: true})) replacement attempts aside — concretely: plainObs[refCount](), or refCount()(plainObs) applied to a non-connectable source; also after refactoring removed the publish step.","commonSituations":"RxJS 6->7->Next migrations where multicast/publish was dropped but refCount remained in the pipe; applying refCount to the output of operators that return plain Observables.","solutions":["Apply publish/publishReplay first: source[publishReplay]()[refCount]()","Replace the whole pattern with shareReplay({ bufferSize: n, refCount: true }) which needs no ConnectableObservable","Check instanceof ConnectableObservable before calling refCount in generic code"],"exampleFix":"// before\nsource[refCount]();\n// after\nsource[publishReplay](1)[refCount]();\n// or: source[shareReplay]({ bufferSize: 1, refCount: true })","handlingStrategy":"type-guard","validationCode":"if (!(source instanceof ConnectableObservable)) {\n  source = source[publishReplay](1);\n}\nsource[refCount]();","typeGuard":"const isConnectable = (o: unknown): o is ConnectableObservable<any> =>\n  o instanceof ConnectableObservable;","tryCatchPattern":null,"preventionTips":["Always pair refCount with publish/publishReplay/multicast","Prefer shareReplay({ refCount: true }) as the modern equivalent","Type streams as ConnectableObservable<T> where refCount is intended"],"tags":["ref-count","connectable-observable","publish","rxjs-next","api-misuse"],"backgroundTag":"wrong-receiver-type","analyzedSha":"54796b38a57e6309f9861e174737479bb3f63f61","analyzedAt":"2026-08-28T10:21:27.410Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}