diff --git a/assert/array_includes.ts b/assert/array_includes.ts index bcc14fefccd8..c228a58addd0 100644 --- a/assert/array_includes.ts +++ b/assert/array_includes.ts @@ -29,7 +29,7 @@ export type ArrayLikeArg = ArrayLike & object; */ export function assertArrayIncludes( actual: ArrayLikeArg, - expected: ArrayLikeArg, + expected: NoInfer>, msg?: string, ) { const missing: unknown[] = []; diff --git a/assert/array_includes_test.ts b/assert/array_includes_test.ts index f23ef4aaf139..e2868597044e 100644 --- a/assert/array_includes_test.ts +++ b/assert/array_includes_test.ts @@ -82,3 +82,12 @@ Deno.test("assertArrayIncludes() type-checks failing cases", () => { // @ts-expect-error both args - 'string' is not assignable to 'ArrayLikeArg'. assertThrows(() => assertArrayIncludes("a", "b")); }); + +// https://github.com/denoland/std/issues/6427 +Deno.test("assertArrayIncludes() fails type checking when element type of expected not assignable to actual", () => { + const ones: 1[] = [1]; + const twos: 2[] = [2]; + + // @ts-expect-error Argument of type '2[]' is not assignable to parameter of type 'ArrayLikeArg<1>'. + assertThrows(() => assertArrayIncludes(ones, twos)); +}); diff --git a/collections/mod.ts b/collections/mod.ts index e76c656ba911..80ef146ee1b6 100644 --- a/collections/mod.ts +++ b/collections/mod.ts @@ -18,7 +18,7 @@ * * assertEquals(intersect(lisaInterests, kimInterests), ["Cooking", "Music"]); * - * assertArrayIncludes(lisaInterests, [sample(lisaInterests)]); + * assertArrayIncludes(lisaInterests, [sample(lisaInterests)!]); * * const cat = { name: "Lulu", age: 3, breed: "Ragdoll" }; * diff --git a/collections/sample.ts b/collections/sample.ts index 0fd36f946f7b..13e746cfded5 100644 --- a/collections/sample.ts +++ b/collections/sample.ts @@ -25,7 +25,7 @@ function randomInteger(lower: number, upper: number): number { * import { assertArrayIncludes } from "@std/assert"; * * const numbers = [1, 2, 3, 4]; - * const random = sample(numbers); + * const random = sample(numbers)!; * * assertArrayIncludes(numbers, [random]); * ``` diff --git a/collections/unstable_sample.ts b/collections/unstable_sample.ts index 5768c0b71ae7..a46d6ee421cf 100644 --- a/collections/unstable_sample.ts +++ b/collections/unstable_sample.ts @@ -18,7 +18,7 @@ * import { assertArrayIncludes } from "@std/assert"; * * const numbers = [1, 2, 3, 4]; - * const random = sample(numbers); + * const random = sample(numbers)!; * * assertArrayIncludes(numbers, [random]); * ``` diff --git a/random/sample.ts b/random/sample.ts index 6c5c83d4a624..94ed920e3e02 100644 --- a/random/sample.ts +++ b/random/sample.ts @@ -38,7 +38,7 @@ export type SampleOptions = RandomOptions & { * import { assertArrayIncludes } from "@std/assert"; * * const numbers = [1, 2, 3, 4]; - * const sampled = sample(numbers); + * const sampled = sample(numbers)!; * * assertArrayIncludes(numbers, [sampled]); * ``` diff --git a/random/sample_test.ts b/random/sample_test.ts index 9ce245fb18fc..2d6f034c1057 100644 --- a/random/sample_test.ts +++ b/random/sample_test.ts @@ -31,7 +31,7 @@ Deno.test({ name: "sample() handles array of numbers", fn() { const input = [1, 2, 3]; - const actual = sample(input); + const actual = sample(input)!; assertArrayIncludes(input, [actual]); }, @@ -50,7 +50,7 @@ Deno.test({ age: 24, }, ]; - const actual = sample(input); + const actual = sample(input)!; assertArrayIncludes(input, [actual]); },