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 be Writable
.safeCastTo
- Cast a value to the given type safely.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()
.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.