upstash/context7 · error · TypeError

timeout must be a positive number or false

Error message

timeout must be a positive number or false

What it means

TypeError thrown by validateTimeout when the `timeout` option is neither `false` (disabled) nor a positive finite number. The SDK requires timeout to be a millisecond value > 0 or explicitly false to disable it.

Source

Thrown at packages/sdk/src/http/abort.ts:11

import { Context7Error } from "@error";

export type AbortState = {
  signal?: AbortSignal;
  timedOut: () => boolean;
  cleanup: () => void;
};

export function validateTimeout(timeout: number | false): void {
  if (timeout !== false && (!Number.isFinite(timeout) || timeout <= 0)) {
    throw new TypeError("timeout must be a positive number or false");
  }
}

export function resolveSignal(signal?: AbortSignal | (() => AbortSignal)): AbortSignal | undefined {
  return typeof signal === "function" ? signal() : signal;
}

export function createAbortState(
  signals: Array<AbortSignal | undefined>,
  timeout: number | false
): AbortState {
  validateTimeout(timeout);

  const activeSignals = [
    ...new Set(signals.filter((signal): signal is AbortSignal => signal !== undefined)),
  ];
  if (timeout === false && activeSignals.length === 0) {
    return { timedOut: () => false, cleanup: () => undefined };

View on GitHub (pinned to 80e681a507)

Solutions

  1. Pass a positive finite number of milliseconds, e.g. timeout: 10000.
  2. To disable the timeout entirely, pass timeout: false, not 0.
  3. If loading from env/config, coerce and validate: Number.isFinite(v) && v > 0 before constructing the client.
  4. Use Number(rawValue) and guard against NaN when parsing user-supplied values.

Example fix

// before
const client = new Context7Client({ timeout: Number(process.env.TIMEOUT) }); // NaN -> TypeError
// after
const t = Number(process.env.TIMEOUT);
const client = new Context7Client({ timeout: Number.isFinite(t) && t > 0 ? t : false });
Defensive patterns

Strategy: validation

Validate before calling

function assertTimeout(t: unknown): asserts t is number | false {
  if (!(t === false || (typeof t === 'number' && Number.isFinite(t) && t > 0))) {
    throw new TypeError(`timeout must be a positive number or false, got ${String(t)}`);
  }
}

Type guard

function isValidTimeout(t: unknown): t is number | false {
  return t === false || (typeof t === 'number' && Number.isFinite(t) && t > 0);
}

Try / catch

try {
  const client = new Context7Client({ timeout: parsedTimeout });
} catch (e) {
  if (e instanceof TypeError && /timeout/.test(e.message)) {
    console.error('Bad timeout config, falling back to default');
  }
  throw e;
}

Prevention

When it happens

Trigger: Constructing a Context7 client (or calling createAbortState) with timeout: 0, timeout: -1000, timeout: Infinity, timeout: NaN, or a non-number truthy value like a string "5000" that is not false.

Common situations: Loading timeout from environment variables or config files as a string ("5000"), computing it to 0 or NaN via parsing, or intending to disable it with 0 instead of false.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of upstash/context7@80e681a507 (2026-09-08). Data as JSON: /api/errors/c1d750953fb09342. Report an issue: GitHub.