Skip to content

Essential utilities for TypeScript projects

License

Notifications You must be signed in to change notification settings

sindresorhus/ts-extras

Repository files navigation

ts-extras npm dependents npm downloads

Essential utilities for TypeScript projects

Ideas for additional essential utilities welcome. Type-only utilities belong in type-fest.

Install

npm install ts-extras

Usage

import {isDefined} from 'ts-extras';

[1, undefined, 2].filter(isDefined);
//=> [1, 2]

API

General

Type guard

  • isDefined - Check whether a value is defined (not undefined).
  • isPresent - Check whether a value is present (not null or undefined).
  • isEmpty - Check whether an array is empty.
  • assertError - Assert that the given value is an Error.
  • isInfinite - Check whether a value is infinite.

Improved builtin

  • arrayIncludes - An alternative to Array#includes() that properly acts as a type guard.
  • setHas - An alternative to Set#has() that properly acts as a type guard.
  • objectKeys - A strongly-typed version of Object.keys().
  • objectEntries - A strongly-typed version of Object.entries().
  • objectFromEntries - A strongly-typed version of Object.fromEntries().
  • objectHasOwn - A strongly-typed version of Object.hasOwn().
  • isFinite - A strongly-typed version of Number.isFinite().
  • isInteger - A strongly-typed version of Number.isInteger().
  • isSafeInteger - A strongly-typed version of Number.isSafeInteger().

FAQ

What is the difference between this and type-fest?

The type-fest package contains only types, meaning they are only used at compile-time and nothing is ever compiled into actual JavaScript code. This package contains functions that are compiled into JavaScript code and used at runtime.

Related

  • type-fest - A collection of essential TypeScript types
  • is - Type guards for any situation