Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Arrayable type #270 #935

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type {UndefinedOnPartialDeep} from './source/undefined-on-partial-deep';
export type {ReadonlyDeep} from './source/readonly-deep';
export type {LiteralUnion} from './source/literal-union';
export type {Promisable} from './source/promisable';
export type {Arrayable} from './source/arrayable';
export type {Opaque, UnwrapOpaque, Tagged, GetTagMetadata, UnwrapTagged} from './source/opaque';
export type {InvariantOf} from './source/invariant-of';
export type {SetOptional} from './source/set-optional';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;

### Array

- [`Arrayable`](source/arrayable.d.ts) - Create a type that represents either the value or an array of the value.
- [`Includes`](source/includes.d.ts) - Returns a boolean for whether the given array includes the given item.
- [`Join`](source/join.d.ts) - Join an array of strings and/or numbers using the given string as a delimiter.
- [`ArraySlice`](source/array-slice.d.ts) - Returns an array slice of a given range, just like `Array#slice()`.
Expand Down
26 changes: 26 additions & 0 deletions source/arrayable.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
Create a type that represents either the value or an array of the value.

@see Promisable

@example
```
import type {Arrayable} from 'type-fest';

function bundle(input: string, output: Arrayable<string>) {
const outputList = Array.isArray(output) ? output : [output];

// …

for (const output of outputList) {
console.log(`write to: ${output}`);
}
}

bundle('src/index.js', 'dist/index.js');
bundle('src/index.js', ['dist/index.cjs', 'dist/index.mjs']);
```

@category Array
*/
export type Arrayable<T> = T | readonly T[];
9 changes: 9 additions & 0 deletions test-d/arrayable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {expectType} from 'tsd';
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[][]);