Skip to content

Commit

Permalink
fix: arrayble is unusable
Browse files Browse the repository at this point in the history
  • Loading branch information
adduserwyw committed Dec 10, 2024
1 parent b24fdd9 commit 42a4ec0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion source/arrayable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ bundle('src/index.js', ['dist/index.cjs', 'dist/index.mjs']);
@category Array
*/
export type Arrayable<T> = T | readonly T[];
export type Arrayable<T> = T | T[];
23 changes: 19 additions & 4 deletions test-d/arrayable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ import type {Arrayable} from '../index';

declare const unknown: unknown;

expectType<Arrayable<string>>(unknown as string | readonly string[]);
expectType<Arrayable<string | {foo: number}>>(unknown as (string | {foo: number}) | ReadonlyArray<string | {foo: number}>);
expectType<Arrayable<never>>(unknown as /* never | */ readonly never[]);
expectType<Arrayable<string[]>>(unknown as string[] | readonly string[][]);
expectType<Arrayable<string>>(unknown as string | string[]);
expectType<Arrayable<string | {foo: number}>>(unknown as (string | {foo: number}) | Array<string | {foo: number}>);
expectType<Arrayable<never>>(unknown as /* never | */ never[]);
expectType<Arrayable<string[]>>(unknown as string[] | string[][]);

// Test for issue https://github.com/sindresorhus/type-fest/issues/952
type Item = number;
function castArray1(value: Arrayable<Item>): Item[] {
return Array.isArray(value) ? value : [value];
}

expectType<Item[]>(unknown as ReturnType<typeof castArray1>);

function castArray2<T>(value: Arrayable<T>): T[] {
return Array.isArray(value) ? value : [value];
}

expectType<number[]>(castArray2(1));
expectType<number[]>(castArray2([1, 2, 3]));

0 comments on commit 42a4ec0

Please sign in to comment.