-
-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
6ed388f
commit 9aabcb9
Showing
4 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[][]); |