diff --git a/source/arrayable.d.ts b/source/arrayable.d.ts index e066e5d13..54aed8f39 100644 --- a/source/arrayable.d.ts +++ b/source/arrayable.d.ts @@ -13,7 +13,7 @@ function bundle(input: string, output: Arrayable) { // … for (const output of outputList) { - console.log(`write to: ${output}`); + console.log(`write to: ${output}`); } } @@ -23,4 +23,7 @@ bundle('src/index.js', ['dist/index.cjs', 'dist/index.mjs']); @category Array */ -export type Arrayable = T | readonly T[]; +export type Arrayable = +T +// TODO: Use `readonly T[]` when this issue is resolved: https://github.com/microsoft/TypeScript/issues/17002 +| T[]; diff --git a/test-d/arrayable.ts b/test-d/arrayable.ts index 671929795..0d83d86fd 100644 --- a/test-d/arrayable.ts +++ b/test-d/arrayable.ts @@ -3,7 +3,22 @@ import type {Arrayable} from '../index'; declare const unknown: unknown; -expectType>(unknown as string | readonly string[]); -expectType>(unknown as (string | {foo: number}) | ReadonlyArray); -expectType>(unknown as /* never | */ readonly never[]); -expectType>(unknown as string[] | readonly string[][]); +expectType>(unknown as string | string[]); +expectType>(unknown as (string | {foo: number}) | Array); +expectType>(unknown as /* never | */ never[]); +expectType>(unknown as string[] | string[][]); + +// Test for issue https://github.com/sindresorhus/type-fest/issues/952 +type Item = number; +function castArray1(value: Arrayable): Item[] { + return Array.isArray(value) ? value : [value]; +} + +expectType(unknown as ReturnType); + +function castArray2(value: Arrayable): T[] { + return Array.isArray(value) ? value : [value]; +} + +expectType(castArray2(1)); +expectType(castArray2([1, 2, 3]));