denoland/deno · error · TypeError

Invalid mutation type

Error message

Invalid mutation type

What it means

mutate() switches over mutation.type and recognizes only delete, set, sum, min and max; anything else falls through to default and throws TypeError('Invalid mutation type'). There is no 'insert' or 'upsert' — 'set' is the unconditional write, and check() is the separate optimistic-concurrency mechanism.

Solutions

  1. Use only the five literal types; for upsert semantics use "set" (it overwrites unconditionally).
  2. Type mutation arrays as Deno.KvMutation[] — the string-literal union catches typos at compile time.
  3. Keep types in an as-const object: const MUT = { SET: "set", DELETE: "delete", ... } as const, and reference MUT.* everywhere.

Example fix

// before
kv.atomic().mutate({ type: "insert", key, value }); // Invalid mutation type

// after
kv.atomic().mutate({ type: "set", key, value });
Defensive patterns

Strategy: type-guard

Validate before calling

const MUTATION_TYPES = new Set(["delete", "set", "sum", "min", "max"]);
if (!MUTATION_TYPES.has(m.type)) {
  throw new TypeError(`unknown mutation type '${m.type}' (allowed: ${[...MUTATION_TYPES].join(", ")})`);
}

Type guard

const isMutationType = (t: unknown): t is "delete" | "set" | "sum" | "min" | "max" =>
  t === "delete" || t === "set" || t === "sum" || t === "min" || t === "max";

Prevention

When it happens

Trigger: `mutate({ type: "insert", key, value })`, "upsert", "update", casing mistakes like "SET" or "Delete", or a type string assembled dynamically from config/data with a typo.

Common situations: Porting SQL CRUD vocabulary (insert/update/upsert) to Deno.Kv; constants defined outside TypeScript's checking (plain strings in JSON config); mutation objects typed as any so the union never runs.

Related errors


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

Appendix: source

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

          }
          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,
    ]);
    return this;
  }

  min(key: Deno.KvKey, n: bigint): this {
    ArrayPrototypePush(this.#mutations, [

View on GitHub (pinned to 9ad36f7a2c)