Essential utilities for TypeScript projects
Ideas for additional essential utilities welcome. Type-only utilities belong in type-fest.
npm install ts-extras
import {isDefined} from 'ts-extras';
[1, undefined, 2].filter(isDefined);
//=> [1, 2]
General
-
asWritable
- Cast the given value to beWritable
. -
safeCastTo
- Cast a value to the given type safely.
Type guard
-
isDefined
- Check whether a value is defined (notundefined
). -
isPresent
- Check whether a value is present (notnull
orundefined
). -
isEmpty
- Check whether an array is empty. -
assertError
- Assert that the given value is anError
. -
isInfinite
- Check whether a value is infinite.
Improved builtin
-
arrayIncludes
- An alternative toArray#includes()
that properly acts as a type guard. -
setHas
- An alternative toSet#has()
that properly acts as a type guard. -
objectKeys
- A strongly-typed version ofObject.keys()
. -
objectEntries
- A strongly-typed version ofObject.entries()
. -
objectFromEntries
- A strongly-typed version ofObject.fromEntries()
. -
objectHasOwn
- A strongly-typed version ofObject.hasOwn()
. -
isFinite
- A strongly-typed version ofNumber.isFinite()
. -
isInteger
- A strongly-typed version ofNumber.isInteger()
. -
isSafeInteger
- A strongly-typed version ofNumber.isSafeInteger()
.
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.