ts-array-length
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

ts-array-length

TypeScript utilities for dealing with array length. Of course, type predicates inside.

Useful for codebase with noUncheckedIndexedAccess turned on.

Installation

npm i -D ts-array-length

API: functions

hasLength(arr, len)

Returns true if arr.length === len.

Example

// const arr: string[]

if (hasLength(arr, 1)) {
  // arr: readonly [string]
  const str: string = arr[0];
}

hasMinLength(arr, len)

Returns true if arr.length >= len.

Example

// const arr: string[]

if (hasMinLength(arr, 1)) {
  // arr: readonly [string, ...string[]]
  const str: string = arr[0];
}

isNonEmpty(arr)

Returns true if arr.length >= 1.

Example

// const arr: string[]

if (isNonEmpty(arr)) {
  // arr: readonly [string, ...string[]]
  const str: string = arr[0];
}

API: utility types

ReadonlyArrayExactLength<T, N>

Tuple type whose element type is T and has exact length N.

// readonly [string, string, string]
type SSS = ReadonlyArrayExactLength<string, 3>;

ReadonlyArrayMinLength<T, N>

Tuple type whose element type is T and has at least N elements.

// readonly [string, string, string, ...string[]]
type SSS = ReadonlyArrayMinLength<string, 3>;

License

MIT

/ts-array-length/

    Package Sidebar

    Install

    npm i ts-array-length

    Weekly Downloads

    1,158

    Version

    0.1.3

    License

    MIT

    Unpacked Size

    37.4 kB

    Total Files

    43

    Last publish

    Collaborators

    • uhyo