Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EmptyArray and IsEmptyArray types #931

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type {EmptyObject, IsEmptyObject} from './source/empty-object';
export type {IfEmptyObject} from './source/if-empty-object';
export type {NonEmptyObject} from './source/non-empty-object';
export type {UnknownRecord} from './source/unknown-record';
export type {EmptyArray, IsEmptyArray} from './source/empty-array';
export type {UnknownArray} from './source/unknown-array';
export type {Except} from './source/except';
export type {TaggedUnion} from './source/tagged-union';
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Click the type names for complete docs.
- [`EmptyObject`](source/empty-object.d.ts) - Represents a strictly 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 `{}`.
- [`EmptyArray`](source/empty-array.d.ts) - Represents a strictly empty array, the `[]` value.
- [`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>`.
Expand Down Expand Up @@ -242,6 +243,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
- [`IsNever`](source/is-never.d.ts) - Returns a boolean for whether the given type is `never`. (Conditional version: [`IfNever`](source/if-never.d.ts))
- [`IsUnknown`](source/is-unknown.d.ts) - Returns a boolean for whether the given type is `unknown`. (Conditional version: [`IfUnknown`](source/if-unknown.d.ts))
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a boolean for whether the type is strictly equal to an empty plain object, the `{}` value. (Conditional version: [`IfEmptyObject`](source/if-empty-object.d.ts))
- [`IsEmptyArray`](source/empty-array.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty array, the `[]` value.
- [`IsNull`](source/is-null.d.ts) - Returns a boolean for whether the given type is `null`. (Conditional version: [`IfNull`](source/if-null.d.ts))

### JSON
Expand Down
32 changes: 32 additions & 0 deletions source/empty-array.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
Represents a strictly empty array, the `[]` value.

@example
```
import type {EmptyArray} from 'type-fest';

const bar1: EmptyArray = []; // Pass
const bar2: EmptyArray = [1]; // Fail
const bar3: EmptyArray = {}; // Fail
```

@category Array
*/
export type EmptyArray = never[];

/**
Returns a `boolean` for whether the type is strictly equal to an empty array, the `[]` value.

@example
```
import type {IsEmptyArray} from 'type-fest';

type Pass = IsEmptyArray<[]>; //=> true
type Fail = IsEmptyArray<[1]>; //=> false
type Fail = IsEmptyArray<{}>; //=> false
```

@see EmptyArray
@category Array
*/
export type IsEmptyArray<T> = T extends EmptyArray ? true : false;
39 changes: 39 additions & 0 deletions test-d/empty-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {expectAssignable, expectNotAssignable, expectType} from 'tsd';
import type {EmptyArray, IsEmptyArray} from '../index';

declare let foo: EmptyArray;

expectAssignable<EmptyArray>([]);
expectNotAssignable<EmptyArray>([1]);
expectNotAssignable<EmptyArray>([undefined]);
expectNotAssignable<EmptyArray>({});

foo = [];
foo = [...[]]; // eslint-disable-line unicorn/no-useless-spread
foo = [...new Set([])];
foo.slice(1);
foo.splice(1);
const _length = foo.length;

// @ts-expect-error
foo.push(1);
// @ts-expect-error
foo.fill(1);
// @ts-expect-error
foo.unshift(1);
// @ts-expect-error
foo = [1];
// @ts-expect-error
foo = [...[1]]; // eslint-disable-line unicorn/no-useless-spread
// @ts-expect-error
foo = [...new Set([1])];
// @ts-expect-error
foo = null;
// @ts-expect-error
foo.bar = 42;
// @ts-expect-error
foo.bar = [];

expectType<IsEmptyArray<[]>>(true);
expectType<IsEmptyArray<[1]>>(false);
expectType<IsEmptyArray<typeof foo>>(true);