From 4e8f0e7bb80300f4ad06483436d154e7fe10b5ab Mon Sep 17 00:00:00 2001 From: Jesse Jackson Date: Thu, 3 Mar 2022 23:31:19 -0600 Subject: [PATCH] feat(testing/asserts): use assertion signature for "assertStrictEquals" (#1984) --- async/debounce_test.ts | 15 +++++++++------ testing/asserts.ts | 14 ++------------ testing/asserts_test.ts | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/async/debounce_test.ts b/async/debounce_test.ts index f2f35d57d..e26d83bb7 100644 --- a/async/debounce_test.ts +++ b/async/debounce_test.ts @@ -45,12 +45,15 @@ Deno.test("[async] debounce: flushed", function () { Deno.test("[async] debounce: with params & context", async function () { const params: Array = []; - const d = debounce(function (param1: string, param2: number) { - assertEquals(d.pending, false); - params.push(param1); - params.push(param2); - assertStrictEquals(d, this); - }, 100); + const d: DebouncedFunction<[string, number]> = debounce( + function (param1: string, param2: number) { + assertEquals(d.pending, false); + params.push(param1); + params.push(param2); + assertStrictEquals(d, this); + }, + 100, + ); // @ts-expect-error Argument of type 'number' is not assignable to parameter of type 'string'. d(1, 1); d("foo", 1); diff --git a/testing/asserts.ts b/testing/asserts.ts index e06959358..c26e2b28c 100644 --- a/testing/asserts.ts +++ b/testing/asserts.ts @@ -323,21 +323,11 @@ export function assertNotEquals( * assertStrictEquals(1, 2) * ``` */ -export function assertStrictEquals( - actual: unknown, - expected: unknown, - msg?: string, -): void; export function assertStrictEquals( - actual: T, + actual: unknown, expected: T, msg?: string, -): void; -export function assertStrictEquals( - actual: unknown, - expected: unknown, - msg?: string, -): void { +): asserts actual is T { if (actual === expected) { return; } diff --git a/testing/asserts_test.ts b/testing/asserts_test.ts index 49d2c6d75..106663320 100644 --- a/testing/asserts_test.ts +++ b/testing/asserts_test.ts @@ -874,6 +874,30 @@ Deno.test({ }, }); +Deno.test({ + name: "strict types test", + fn(): void { + const x = { number: 2 }; + + const y = x as Record; + const z = x as unknown; + + // y.number; + // ~~~~~~ + // Property 'number' does not exist on type 'Record'.deno-ts(2339) + + assertStrictEquals(y, x); + y.number; // ok + + // z.number; + // ~ + // Object is of type 'unknown'.deno-ts(2571) + + assertStrictEquals(z, x); + z.number; // ok + }, +}); + Deno.test({ name: "strict pass case", fn(): void {