Essential utilities for TypeScript projects
Ideas for additional essential utilities welcome. Type-only utilities belong in type-fest.
npm install ts-extrasimport {isDefined} from 'ts-extras';
[1, undefined, 2].filter(isDefined);
//=> [1, 2]General
asWritable- Cast the given value to beWritable.safeCastTo- Constrain a value to the given type safely.
Type guard
isDefined- Check whether a value is defined (notundefined).isEqualType- Check if two types are equal at compile time.isPresent- Check whether a value is present (notnullnorundefined).isEmpty- Check whether an array is empty.isFinite- A strongly-typed version ofNumber.isFinite().isInfinite- Check whether a value is infinite.isInteger- A strongly-typed version ofNumber.isInteger().isSafeInteger- A strongly-typed version ofNumber.isSafeInteger().keyIn- Check if a key is in an object and narrow the key to the object's keys.not- Invert a type predicate function.objectHasIn- Check if an object has a property (including inherited) and narrow the object type.assertDefined- Assert that the given value is defined, meaning it is notundefined.assertPresent- Assert that the given value is present (non-nullable), meaning it is neithernullnorundefined.assertError- Assert that the given value is anError.
Improved builtin
arrayAt- A strongly-typed version ofArray#at()with improved tuple support (supports-1and positive literal indices for tuples).arrayConcat- A strongly-typed version ofArray#concat()that properly handles arrays of different types.arrayFirst- Return the first item of an array with stronger typing for tuples.arrayIncludes- A strongly-typed version ofArray#includes()that properly acts as a type guard.arrayLast- Return the last item of an array with stronger typing for tuples.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().setHas- A strongly-typed version ofSet#has()that properly acts as a type guard.stringSplit- A strongly-typed version ofString#split()that returns a tuple for literal strings.
These functions solve different problems despite all checking property existence:
keyIn - Key narrowing for union types:
- Uses the
inoperator, checking the prototype chain - Narrows the key variable to only keys that exist in the object
- Best for: "Which of these possible keys actually exists?"
- Guards against
__proto__andconstructorfor security
objectHasIn - Object narrowing with prototype chain:
- Uses the
inoperator, checking the prototype chain - Narrows the object type to include the checked property
- Best for: "Can I safely access this property (including inherited)?"
- Guards against
__proto__andconstructorfor security
objectHasOwn - Object narrowing for own properties:
- Uses
Object.hasOwn(), checking only own properties - Narrows the object type to include the checked property
- Best for: "Can I safely access this own property on this object?"
// keyIn - narrows the key (prototype chain)
const key = 'foo' as 'foo' | 'bar' | 'baz';
if (keyIn(object, key)) {
// `key` is now: 'foo' | 'bar' (only existing keys)
console.log(object[key]); // Safe
}
// objectHasIn - narrows the object (prototype chain)
const data: unknown = {foo: 1};
if (objectHasIn(data, 'toString')) {
// `data` is now: unknown & {toString: unknown}
console.log(data.toString); // Safe (inherited method)
}
// objectHasOwn - narrows the object (own properties only)
if (objectHasOwn(data, 'foo')) {
// `data` is now: unknown & {foo: unknown}
console.log(data.foo); // Safe (own property)
}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.
- type-fest - A collection of essential TypeScript types
- is - Type guards for any situation
- camelcase-keys - Runtime transformation of object properties to camel-case (like
type-fest'sCamelCasedPropertiesDeep)