import {expectType} from 'tsd'; import type {ArrayElement} from '../index.d.ts'; // Basic array expectType({} as ArrayElement); expectType({} as ArrayElement); // Tuple expectType<1 | 2 | 3>({} as ArrayElement<[1, 2, 3]>); expectType<'a' | 'b' | 'c'>({} as ArrayElement<['a', 'b', 'c']>); // Const tuple expectType<'foo' | 'bar' | 'baz'>({} as ArrayElement); expectType<1 | 2 | 3>({} as ArrayElement); // Mixed types expectType({} as ArrayElement<[string, number]>); expectType({} as ArrayElement<[string, number]>); // Readonly array expectType({} as ArrayElement); expectType<1 | 2 | 3>({} as ArrayElement); // Empty array expectType({} as ArrayElement<[]>); expectType({} as ArrayElement); // Union of arrays expectType<1 | 2 | 3 | 4>({} as ArrayElement<[1, 2] | [3, 4]>); // Function use case declare function getRandomElement(array: T): ArrayElement; expectType(getRandomElement([1, 2, 3])); expectType<'foo' | 'bar' | 'baz'>(getRandomElement(['foo', 'bar', 'baz'] as const)); expectType(getRandomElement(['foo', 'bar', 'baz'])); // Edge cases expectType({} as ArrayElement); expectType({} as ArrayElement); expectType({} as ArrayElement); // Non-arrays return never expectType({} as ArrayElement); expectType({} as ArrayElement<{a: string}>); // Optional and rest elements expectType<1 | 2 | 3 | undefined>(1 as ArrayElement<[1, 2, 3?]>); expectType(1 as ArrayElement<[string, ...number[]]>); expectType<1 | 2 | string>(1 as ArrayElement<[1, 2, ...string[]]>); expectType<1 | 2 | undefined | string>(1 as ArrayElement<[1, 2?, ...string[]]>);