Skip to content

Commit

Permalink
Add UnknownArray type (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa committed Nov 4, 2023
1 parent 5eeac02 commit 30aa0ad
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -9,6 +9,7 @@ export type {KeysOfUnion} from './source/keys-of-union';
export type {EmptyObject, IsEmptyObject} from './source/empty-object';
export type {NonEmptyObject} from './source/non-empty-object';
export type {UnknownRecord} from './source/unknown-record';
export type {UnknownArray} from './source/unknown-array';
export type {Except} from './source/except';
export type {TaggedUnion} from './source/tagged-union';
export type {Writable} from './source/writable';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -113,6 +113,7 @@ Click the type names for complete docs.
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value.
- [`NonEmptyObject`](source/non-empty-object.d.ts) - Represents an object with at least 1 non-optional key.
- [`UnknownRecord`](source/unknown-record.d.ts) - Represents an object with `unknown` value. You probably want this instead of `{}`.
- [`UnknownArray`](source/unknown-array.d.ts) - Represents an array with `unknown` value.
- [`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).
- [`Writable`](source/writable.d.ts) - Create a type that strips `readonly` from the given type. Inverse of `Readonly<T>`.
- [`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.
Expand Down
25 changes: 25 additions & 0 deletions source/unknown-array.d.ts
@@ -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[];
34 changes: 34 additions & 0 deletions test-d/unknown-array.ts
@@ -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);

0 comments on commit 30aa0ad

Please sign in to comment.