denoland/deno · error · TypeError

Invalid mutation ' ' without value

Error message

Invalid mutation '${type}' without value

What it means

mutate() requires set/sum/min/max mutations to carry a value as an own property (`ObjectHasOwn(mutation, "value")`). Omitting the key entirely throws TypeError(`Invalid mutation '<type>' without value`) naming the offending type. The check is key presence, not value truthiness — but {value: undefined} will fail later during serialization, so always supply a real value.

Solutions

  1. Always include an explicit value: { type: "sum", key, value: 1n }.
  2. Prefer the dedicated helpers .set(key, v), .sum(key, 1n), .min(key, v), .max(key, v) whose signatures force the argument.
  3. Annotate arrays as Deno.KvMutation[] so TypeScript reports the missing value at compile time.

Example fix

// before
kv.atomic().mutate({ type: "sum", key: ["hits"] }); // Invalid mutation 'sum' without value

// after
kv.atomic().mutate({ type: "sum", key: ["hits"], value: 1n });
// or simply: kv.atomic().sum(["hits"], 1n)
Defensive patterns

Strategy: type-guard

Validate before calling

for (const m of mutations) {
  if (m.type !== "delete" && !Object.hasOwn(m, "value")) {
    throw new TypeError(`mutation ${m.type} on ${m.key} is missing value`);
  }
}

Type guard

type FullMutation =
  | { type: "delete"; key: Deno.KvKey }
  | { type: "set" | "sum" | "min" | "max"; key: Deno.KvKey; value: unknown };

const isFullMutation = (m: FullMutation): boolean =>
  m.type === "delete" || Object.hasOwn(m, "value");

Prevention

When it happens

Trigger: `kv.atomic().mutate({ type: "sum", key })` with no value; object shorthand or destructuring that drops the value field ({type, key} spread from a source without value); conditional mutation building where the value assignment branch was skipped.

Common situations: Assuming sum/min/max default to 0 when omitted; porting counter increments from an API where the amount was optional; building mutations via spread from partial records.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/c2503b5b8d380598. Report an issue: GitHub.

Appendix: source

Thrown at ext/kv/01_db.ts:499

      switch (mutation.type) {
        case "delete":
          type = "delete";
          if (mutation.value) {
            throw new TypeError("Invalid mutation 'delete' with value");
          }
          break;
        case "set":
          if (typeof mutation.expireIn === "number") {
            expireIn = mutation.expireIn;
          }
          validateExpireIn(expireIn);
          /* falls through */
        case "sum":
        case "min":
        case "max":
          type = mutation.type;
          if (!ObjectHasOwn(mutation, "value")) {
            throw new TypeError(`Invalid mutation '${type}' without value`);
          }
          value = serializeValue(mutation.value);
          break;
        default:
          throw new TypeError("Invalid mutation type");
      }
      ArrayPrototypePush(this.#mutations, [key, type, value, expireIn]);
    }
    return this;
  }

  sum(key: Deno.KvKey, n: bigint): this {
    ArrayPrototypePush(this.#mutations, [
      key,
      "sum",
      serializeValue(new KvU64(n)),
      undefined,
    ]);

View on GitHub (pinned to 9ad36f7a2c)