{"record":{"id":"2e26238bd3f5edb6","repo":"ReactiveX/rxjs","slug":"argument-out-of-range","errorCode":null,"errorMessage":"argument out of range","messagePattern":"argument out of range","errorType":"exception","errorClass":"ArgumentOutOfRangeError","httpStatus":null,"severity":"error","filePath":"packages/rxjs/src/element-at.ts","lineNumber":17,"sourceCode":"import { create } from './create.js';\nimport { ArgumentOutOfRangeError } from './argument-out-of-range-error.js';\nimport '@rxjs/observable-polyfill';\nimport { subscribeToSource } from './util/observable-helpers.js';\n\nexport const elementAt: unique symbol = Symbol('elementAt');\n\ndeclare global {\n  interface Observable<T> {\n    [elementAt](index: number): Observable<T>;\n    [elementAt]<D>(index: number, defaultValue: D): Observable<T | D>;\n  }\n}\n\nObservable.prototype[elementAt] = function <T, D>(this: Observable<T>, index: number, ...defaultValue: [] | [D]): Observable<T | D> {\n  if (index < 0) {\n    throw new ArgumentOutOfRangeError();\n  }\n\n  const hasDefault = defaultValue.length === 1;\n  return this[create]((subscriber) => {\n    let count = 0;\n    subscribeToSource(this, subscriber, {\n      next: (value) => {\n        if (count === index) {\n          subscriber.next(value);\n          subscriber.complete();\n          return;\n        }\n        count++;\n      },\n      complete: () => {\n        if (hasDefault) {\n          subscriber.next(defaultValue[0]);\n          subscriber.complete();","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/ReactiveX/rxjs/blob/54796b38a57e6309f9861e174737479bb3f63f61/packages/rxjs/src/element-at.ts#L1-L35","documentation":"elementAt(index) throws ArgumentOutOfRangeError ('argument out of range') immediately when index is negative — a negative index can never match a zero-based emission, so it fails fast at call time rather than erroring per-subscriber. (A separate out-of-range error occurs at completion when no default is supplied.)","triggerScenarios":"observable[elementAt](-1), or elementAt(someIndex) where someIndex is computed (e.g. list.length - counter) and goes negative on empty input.","commonSituations":"Off-by-one bugs with computed indices (length - 1 on an empty collection), porting code that assumed Python-style negative indexing, or clamping logic that never runs before the call.","solutions":["Guard the index before calling: Math.max(0, index)","Use first()/last() operators instead of elementAt for those semantics","If the source may be empty, supply a default: elementAt(0, fallback)"],"exampleFix":"// before\nobs[elementAt](items.length - 1); // -1 when items is empty\n// after\nobs[elementAt](Math.max(0, items.length - 1), undefined);","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(index) || index < 0) throw new RangeError('index must be a non-negative integer');\nobs[elementAt](index);","typeGuard":"const isNonNegativeInt = (n: unknown): n is number => Number.isInteger(n) && (n as number) >= 0;","tryCatchPattern":null,"preventionTips":["Clamp computed indices with Math.max(0, index)","Use first/last operators for those semantics","Supply a default value when the source may be empty"],"tags":["element-at","argument-out-of-range","validation","off-by-one"],"backgroundTag":"argument-out-of-range","analyzedSha":"54796b38a57e6309f9861e174737479bb3f63f61","analyzedAt":"2026-08-28T10:21:27.410Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}