Skip to content

Commit

Permalink
Add Arrayable type #270
Browse files Browse the repository at this point in the history
  • Loading branch information
Max10240 committed Aug 11, 2024
1 parent 6ed388f commit f805d07
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
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 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
25 changes: 25 additions & 0 deletions source/arrayable.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
Create a type that represents either the value or 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 | 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 | 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[][]);

0 comments on commit f805d07

Please sign in to comment.