Skip to content

Commit 30aa0ad

Browse files
authoredNov 4, 2023
Add UnknownArray type (#740)
1 parent 5eeac02 commit 30aa0ad

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
 

‎index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type {KeysOfUnion} from './source/keys-of-union';
99
export type {EmptyObject, IsEmptyObject} from './source/empty-object';
1010
export type {NonEmptyObject} from './source/non-empty-object';
1111
export type {UnknownRecord} from './source/unknown-record';
12+
export type {UnknownArray} from './source/unknown-array';
1213
export type {Except} from './source/except';
1314
export type {TaggedUnion} from './source/tagged-union';
1415
export type {Writable} from './source/writable';

‎readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Click the type names for complete docs.
113113
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value.
114114
- [`NonEmptyObject`](source/non-empty-object.d.ts) - Represents an object with at least 1 non-optional key.
115115
- [`UnknownRecord`](source/unknown-record.d.ts) - Represents an object with `unknown` value. You probably want this instead of `{}`.
116+
- [`UnknownArray`](source/unknown-array.d.ts) - Represents an array with `unknown` value.
116117
- [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys).
117118
- [`Writable`](source/writable.d.ts) - Create a type that strips `readonly` from the given type. Inverse of `Readonly<T>`.
118119
- [`WritableDeep`](source/writable-deep.d.ts) - Create a deeply mutable version of an `object`/`ReadonlyMap`/`ReadonlySet`/`ReadonlyArray` type. The inverse of `ReadonlyDeep<T>`. Use `Writable<T>` if you only need one level deep.

‎source/unknown-array.d.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
Represents an array with `unknown` value.
3+
4+
Use case: You want a type that all arrays can be assigned to, but you don't care about the value.
5+
6+
@example
7+
```
8+
import type {UnknownArray} from 'type-fest';
9+
10+
type IsArray<T> = T extends UnknownArray ? true : false;
11+
12+
type A = IsArray<['foo']>;
13+
//=> true
14+
15+
type B = IsArray<readonly number[]>;
16+
//=> true
17+
18+
type C = IsArray<string>;
19+
//=> false
20+
```
21+
22+
@category Type
23+
@category Array
24+
*/
25+
export type UnknownArray = readonly unknown[];

‎test-d/unknown-array.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {expectAssignable, expectError, expectNotAssignable, expectType} from 'tsd';
2+
import type {UnknownArray} from '../index';
3+
4+
declare const foo: readonly [];
5+
declare const bar: {
6+
readonly array: unknown[];
7+
};
8+
9+
expectAssignable<UnknownArray>(foo);
10+
expectAssignable<UnknownArray>(bar.array);
11+
expectAssignable<UnknownArray>([]);
12+
expectAssignable<UnknownArray>(['foo']);
13+
14+
expectNotAssignable<UnknownArray>(null);
15+
expectNotAssignable(undefined);
16+
expectNotAssignable({});
17+
expectNotAssignable({0: 1});
18+
expectNotAssignable(1);
19+
expectNotAssignable(Date);
20+
21+
type IsArray<T> = T extends UnknownArray ? true : false;
22+
23+
declare const string: IsArray<string>;
24+
expectType<false>(string);
25+
declare const array: IsArray<[]>;
26+
expectType<true>(array);
27+
declare const tuple: IsArray<['foo']>;
28+
expectType<true>(tuple);
29+
declare const readonlyArray: IsArray<readonly number[]>;
30+
expectType<true>(readonlyArray);
31+
declare const leadingSpread: IsArray<readonly [number, ...string[]]>;
32+
expectType<true>(leadingSpread);
33+
declare const trailingSpread: IsArray<readonly [...string[], number]>;
34+
expectType<true>(trailingSpread);

0 commit comments

Comments
 (0)
Please sign in to comment.