From 02e01b171f29f4f6c23d738b0756b7d9b7eaa020 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Thu, 30 Mar 2023 22:52:31 +0200 Subject: fix(dts): improve types for the Deno.KV API (#18510) --- cli/tests/unit/kv_test.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'cli/tests') diff --git a/cli/tests/unit/kv_test.ts b/cli/tests/unit/kv_test.ts index 401eba9b6..69eb8bed9 100644 --- a/cli/tests/unit/kv_test.ts +++ b/cli/tests/unit/kv_test.ts @@ -6,6 +6,7 @@ import { assertRejects, assertThrows, } from "./test_util.ts"; +import { assertType, IsExact } from "../../../test_util/std/testing/types.ts"; let isCI: boolean; try { @@ -529,8 +530,10 @@ Deno.test("KvU64 unbox", () => { assertEquals(a.value, 1n); }); -async function collect(iter: Deno.KvListIterator): Promise { - const entries: Deno.KvEntry[] = []; +async function collect( + iter: Deno.KvListIterator, +): Promise[]> { + const entries: Deno.KvEntry[] = []; for await (const entry of iter) { entries.push(entry); } @@ -1134,3 +1137,46 @@ dbTest("operation size limit", async (db) => { "too many mutations (max 10)", ); }); + +// This function is never called, it is just used to check that all the types +// are behaving as expected. +async function _typeCheckingTests() { + const kv = new Deno.Kv(); + + const a = await kv.get(["a"]); + assertType>>(true); + + const b = await kv.get(["b"]); + assertType>>(true); + + const c = await kv.getMany([["a"], ["b"]]); + assertType< + IsExact, Deno.KvEntryMaybe]> + >(true); + + const d = await kv.getMany([["a"], ["b"]] as const); + assertType< + IsExact, Deno.KvEntryMaybe]> + >(true); + + const e = await kv.getMany<[string, number]>([["a"], ["b"]]); + assertType< + IsExact, Deno.KvEntryMaybe]> + >(true); + + const keys: Deno.KvKey[] = [["a"], ["b"]]; + const f = await kv.getMany(keys); + assertType[]>>(true); + + const g = kv.list({ prefix: ["a"] }); + assertType>>(true); + const h = await g.next(); + assert(!h.done); + assertType>>(true); + + const i = kv.list({ prefix: ["a"] }); + assertType>>(true); + const j = await i.next(); + assert(!j.done); + assertType>>(true); +} -- cgit v1.2.3