Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/local/test/foo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expectTypeOf, test } from "vitest";

type Wrapper<T> = T extends { schema: infer S } ? S : never;

export function foo<const T extends readonly { schema: unknown }[]>(
arr: T
): Wrapper<T[number]>[] {
return arr.map((item) => item.schema) as Wrapper<T[number]>[];
}

test("infers union of schema types with literal types", () => {
const result = foo([{ schema: 1 }, { schema: "a" }]);
expectTypeOf(result).toEqualTypeOf<(1 | "a")[]>();
});

test("infers object and boolean schema types with literal types", () => {
const result = foo([{ schema: { x: 42 } }, { schema: true }]);
expectTypeOf(result).toEqualTypeOf<({ readonly x: 42 } | true)[]>();
});

test("infers single-element array with literal type", () => {
const result = foo([{ schema: 123 }]);
expectTypeOf(result).toEqualTypeOf<123[]>();
});

test("preserves literal tuple inference when using const assertions", () => {
const input = [{ schema: 1 }, { schema: "a" }] as const;
const result = foo(input);
expectTypeOf(result).toEqualTypeOf<(1 | "a")[]>();
});
Loading