forked from sindresorhus/type-fest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
70 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
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 = {}; // Fail | ||
const bar3: EmptyArray = null; // 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<{}>; //=> false | ||
type Fail = IsEmptyArray<null>; //=> false | ||
``` | ||
@see EmptyArray | ||
@category Array | ||
*/ | ||
export type IsEmptyArray<T> = T extends EmptyArray ? true : false; |
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,37 @@ | ||
import {expectAssignable, expectType} from 'tsd'; | ||
import type {EmptyArray, IsEmptyArray} from '../index'; | ||
|
||
declare let foo: EmptyArray; | ||
|
||
expectAssignable<never[]>(foo); | ||
expectAssignable<never[]>(foo = []); | ||
|
||
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); |