-
-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 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,25 @@ | ||
/** | ||
Represents an array with `unknown` value. | ||
Use case: You want a type that all arrays can be assigned to, but you don't care about the value. | ||
@example | ||
``` | ||
import type {UnknownArray} from 'type-fest'; | ||
type IsArray<T> = T extends UnknownArray ? true : false; | ||
type A = IsArray<['foo']>; | ||
//=> true | ||
type B = IsArray<readonly number[]>; | ||
//=> true | ||
type C = IsArray<string>; | ||
//=> false | ||
``` | ||
@category Type | ||
@category Array | ||
*/ | ||
export type UnknownArray = readonly unknown[]; |
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,34 @@ | ||
import {expectAssignable, expectError, expectNotAssignable, expectType} from 'tsd'; | ||
import type {UnknownArray} from '../index'; | ||
|
||
declare const foo: readonly []; | ||
declare const bar: { | ||
readonly array: unknown[]; | ||
}; | ||
|
||
expectAssignable<UnknownArray>(foo); | ||
expectAssignable<UnknownArray>(bar.array); | ||
expectAssignable<UnknownArray>([]); | ||
expectAssignable<UnknownArray>(['foo']); | ||
|
||
expectNotAssignable<UnknownArray>(null); | ||
expectNotAssignable(undefined); | ||
expectNotAssignable({}); | ||
expectNotAssignable({0: 1}); | ||
expectNotAssignable(1); | ||
expectNotAssignable(Date); | ||
|
||
type IsArray<T> = T extends UnknownArray ? true : false; | ||
|
||
declare const string: IsArray<string>; | ||
expectType<false>(string); | ||
declare const array: IsArray<[]>; | ||
expectType<true>(array); | ||
declare const tuple: IsArray<['foo']>; | ||
expectType<true>(tuple); | ||
declare const readonlyArray: IsArray<readonly number[]>; | ||
expectType<true>(readonlyArray); | ||
declare const leadingSpread: IsArray<readonly [number, ...string[]]>; | ||
expectType<true>(leadingSpread); | ||
declare const trailingSpread: IsArray<readonly [...string[], number]>; | ||
expectType<true>(trailingSpread); |