{"record":{"id":"387ce6f3f348dca9","repo":"denoland/deno","slug":"deno-kv-can-not-be-constructed-use-deno-openkv-in","errorCode":null,"errorMessage":"Deno.Kv can not be constructed: use Deno.openKv instead","messagePattern":"Deno\\.Kv can not be constructed: use Deno\\.openKv instead","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/kv/01_db.ts","lineNumber":140,"sourceCode":"  value: Uint8Array;\n} | {\n  kind: \"bytes\";\n  value: Uint8Array;\n} | {\n  kind: \"u64\";\n  value: bigint;\n};\n\nconst kvSymbol = Symbol(\"KvRid\");\nconst commitVersionstampSymbol = Symbol(\"KvCommitVersionstamp\");\n\nclass Kv {\n  #rid: number;\n  #isClosed: boolean;\n\n  constructor(rid: number = undefined, symbol: symbol = undefined) {\n    if (kvSymbol !== symbol) {\n      throw new TypeError(\n        \"Deno.Kv can not be constructed: use Deno.openKv instead\",\n      );\n    }\n    this.#rid = rid;\n    this.#isClosed = false;\n  }\n\n  atomic() {\n    return new AtomicOperation(this.#rid);\n  }\n\n  commitVersionstamp(): symbol {\n    return commitVersionstampSymbol;\n  }\n\n  async get(key: Deno.KvKey, opts?: { consistency?: Deno.KvConsistencyLevel }) {\n    const { 0: entries }: [RawKvEntry[]] = await op_kv_snapshot_read(\n      this.#rid,","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/kv/01_db.ts#L122-L158","documentation":"Deno.Kv is a wrapper around a native database resource and cannot be instantiated directly. Its constructor only accepts a private internal symbol (kvSymbol) that Deno.openKv passes internally; any user-side 'new Deno.Kv(...)' fails the symbol check and throws this TypeError. The guard exists so that every Kv instance is bound to a real, opened database handle.","triggerScenarios":"Calling `new Deno.Kv()` or `new Deno.Kv(rid)`, subclassing (`class MyKv extends Deno.Kv` calling `super()`), or `Reflect.construct(Deno.Kv, [])`. Any construction that does not originate from the internal openKv() factory (ext/kv/01_db.ts:59-62, which does `new Kv(rid, kvSymbol)`) hits the check.","commonSituations":"Developers coming from `new Map()`-style APIs or other KV libraries assume a public constructor; building test mocks by instantiating Kv directly; editor autocomplete suggesting the constructor signature while the real entry point is the openKv factory.","solutions":["Use `const kv = await Deno.openKv()` (no path opens the default database; `Deno.openKv(\":memory:\")` gives an isolated in-memory DB).","Pass an explicit path (`await Deno.openKv(\"./app.db\")`) when you need a persistent on-disk database.","For tests or dependency injection, wrap or wrap around the instance returned by openKv instead of subclassing Deno.Kv.","Call `kv.close()` when finished and re-open with openKv rather than trying to re-construct instances."],"exampleFix":"// before\nconst kv = new Deno.Kv(); // TypeError: use Deno.openKv instead\n\n// after\nconst kv = await Deno.openKv(); // or await Deno.openKv(\":memory:\")","handlingStrategy":"validation","validationCode":"let kv: Deno.Kv;\nasync function getKv(): Promise<Deno.Kv> {\n  if (!(kv instanceof Deno.Kv)) kv = await Deno.openKv();\n  return kv;\n}","typeGuard":"const isKv = (v: unknown): v is Deno.Kv => v instanceof Deno.Kv;","tryCatchPattern":"try {\n  const kv = new Deno.Kv();\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes(\"use Deno.openKv\")) {\n    throw new Error(\"Deno.Kv must be obtained via `await Deno.openKv()`\");\n  }\n  throw e;\n}","preventionTips":["Treat Deno.Kv as a capability acquired from Deno.openKv, never a constructor","Inject the opened kv instance into code under test rather than constructing fakes","Close handles explicitly with kv.close() and re-open instead of re-constructing"],"tags":["deno-kv","constructor","typeerror","factory-method"],"backgroundTag":"non-constructable-class","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}