Runtime type validation utilities with TypeScript type guard support for validating objects and values during execution. These utilities are essential for handling unknown data from API requests, event payloads, or external sources in Lambda functions.
Check if a value is a record (plain object).
/**
* Check if a value is a record.
* @param value - The value to check
* @returns Type guard indicating if value is Record<string | number, unknown>
*/
function isRecord(
value: unknown
): value is Record<string | number, unknown>;Usage Example:
import { isRecord } from '@aws-lambda-powertools/commons/typeutils';
const value: unknown = { key: 'value' };
if (isRecord(value)) {
// TypeScript knows value is a record here
const keys = Object.keys(value);
console.log(value.key);
}Check if a value is a string.
/**
* Check if a value is a string.
* @param value - The value to check
* @returns Type guard indicating if value is string
*/
function isString(value: unknown): value is string;Usage Example:
import { isString } from '@aws-lambda-powertools/commons/typeutils';
const value: unknown = 'hello';
if (isString(value)) {
// TypeScript knows value is a string here
console.log(value.toUpperCase());
}Check if a value is a number.
/**
* Check if a value is a number.
* @param value - The value to check
* @returns Type guard indicating if value is number
*/
function isNumber(value: unknown): value is number;Usage Example:
import { isNumber } from '@aws-lambda-powertools/commons/typeutils';
const value: unknown = 42;
if (isNumber(value)) {
// TypeScript knows value is a number here
console.log(value.toFixed(2));
}Check if a value is an integer number.
/**
* Check if a value is an integer number.
* @param value - The value to check
* @returns Type guard indicating if value is an integer number
*/
function isIntegerNumber(value: unknown): value is number;Usage Example:
import { isIntegerNumber } from '@aws-lambda-powertools/commons/typeutils';
const value: unknown = 42;
if (isIntegerNumber(value)) {
// value is a whole number
console.log('Integer:', value);
}Check if a value is truthy according to JavaScript semantics.
/**
* Check if a value is truthy.
* For strings: non-empty string is truthy
* For numbers: non-zero number is truthy
* For booleans: true is truthy
* For arrays: non-empty array is truthy
* For records: non-empty object is truthy
* @param value - The value to check
* @returns Boolean indicating if value is truthy
*/
function isTruthy(value: unknown): boolean;Usage Example:
import { isTruthy } from '@aws-lambda-powertools/commons/typeutils';
const values = ['yes', 0, true, [], { key: 'value' }];
values.forEach(value => {
if (isTruthy(value)) {
console.log('Truthy:', value);
}
});Check if a value is null.
/**
* Check if a value is null.
* @param value - The value to check
* @returns Type guard indicating if value is null
*/
function isNull(value: unknown): value is null;Usage Example:
import { isNull } from '@aws-lambda-powertools/commons/typeutils';
const value: unknown = null;
if (isNull(value)) {
console.log('Value is null');
}Check if a value is null or undefined.
/**
* Check if a value is null or undefined.
* @param value - The value to check
* @returns Type guard indicating if value is null or undefined
*/
function isNullOrUndefined(value: unknown): value is null | undefined;Usage Example:
import { isNullOrUndefined } from '@aws-lambda-powertools/commons/typeutils';
const value: unknown = undefined;
if (isNullOrUndefined(value)) {
console.log('Value is null or undefined');
}Check if a string is undefined, null, or empty (including whitespace-only strings).
/**
* Check if string is undefined, null, or empty (after trimming).
* @param value - The value to check
* @returns Boolean indicating if value is undefined, null, or empty string
*/
function isStringUndefinedNullEmpty(value: unknown): boolean;Usage Example:
import { isStringUndefinedNullEmpty } from '@aws-lambda-powertools/commons/typeutils';
const value = ' ';
if (isStringUndefinedNullEmpty(value)) {
console.log('String is empty or missing');
}Check if a value is a Regular Expression.
/**
* Check if a value is a Regular Expression.
* @param value - The value to check
* @returns Type guard indicating if value is RegExp
*/
function isRegExp(value: unknown): value is RegExp;Usage Example:
import { isRegExp } from '@aws-lambda-powertools/commons/typeutils';
const value: unknown = /^foo.+$/;
if (isRegExp(value)) {
console.log(value.test('foobar')); // true
}Get the type of a value as a string.
/**
* Get the type of a value as a string.
* @param value - The value to check
* @returns Type string: 'array', 'object', 'string', 'number', 'boolean', 'null', or 'unknown'
*/
function getType(value: unknown): string;Usage Example:
import { getType } from '@aws-lambda-powertools/commons/typeutils';
console.log(getType('foo')); // 'string'
console.log(getType(42)); // 'number'
console.log(getType({ key: 'value' })); // 'object'
console.log(getType([1, 2, 3])); // 'array'
console.log(getType(Symbol('foo'))); // 'unknown'Check if two unknown values are strictly equal with deep comparison for arrays and objects.
/**
* Check if two unknown values are strictly equal.
* For arrays: compares each element regardless of order
* For objects: compares each key and value
* For primitives: uses strict equality
* @param left - Left side of comparison
* @param right - Right side of comparison
* @returns Boolean indicating strict equality
*/
function isStrictEqual(left: unknown, right: unknown): boolean;Usage Example:
import { isStrictEqual } from '@aws-lambda-powertools/commons/typeutils';
const left = { key: 'value' };
const right = { key: 'value' };
console.log(isStrictEqual(left, right)); // true
const arr1 = [1, 2, 3];
const arr2 = [3, 2, 1];
console.log(isStrictEqual(arr1, arr2)); // true (order-independent)
console.log(isStrictEqual('foo', 'bar')); // false
console.log(isStrictEqual(42, 42)); // true