All essential TypeScript types in one place providing 70+ utility types and helper functions.
—
Type utilities for function signatures, predicates, and function-related operations. These types help you work with function types in a type-safe manner.
Matches function type with arguments type Args (any[] by default) and return type ReturnType (any by default).
type AnyFunction<Args extends ReadonlyArray<any> = any[], ReturnType = any> =
(...args: Args) => ReturnType;Usage Example:
import type { AnyFunction } from "ts-essentials";
// Generic function type
type GenericHandler = AnyFunction; // (...args: any[]) => any
// Specific function signatures
type StringProcessor = AnyFunction<[string], string>; // (arg: string) => string
type NumberValidator = AnyFunction<[number], boolean>; // (arg: number) => boolean
type EventHandler = AnyFunction<[Event], void>; // (arg: Event) => void
// Higher-order function utilities
function createMiddleware<F extends AnyFunction>(
originalFn: F,
middleware: (args: Parameters<F>) => void
): F {
return ((...args: Parameters<F>) => {
middleware(args);
return originalFn(...args);
}) as F;
}
// Usage
const add: AnyFunction<[number, number], number> = (a, b) => a + b;
const loggedAdd = createMiddleware(add, ([a, b]) => {
console.log(`Adding ${a} + ${b}`);
});Matches type constraint for type guard, meaning first argument is used in return type and return type is type predicate.
type PredicateFunction = <T, U extends T>(arg: T) => arg is U;Usage Example:
import type { PredicateFunction } from "ts-essentials";
// Type guard functions
const isString: PredicateFunction = (value: unknown): value is string => {
return typeof value === "string";
};
const isNumber: PredicateFunction = (value: unknown): value is number => {
return typeof value === "number";
};
// Generic predicate function factory
function createTypeGuard<T>(
checker: (value: unknown) => boolean
): PredicateFunction {
return (value: unknown): value is T => checker(value);
}
// Usage in filtering
const mixedArray: unknown[] = ["hello", 42, "world", true, 123];
const strings = mixedArray.filter(isString); // string[]
const numbers = mixedArray.filter(isNumber); // number[]
// Custom predicate functions
const isPositive: PredicateFunction = (value: number): value is number => {
return value > 0;
};
const positiveNumbers = [1, -2, 3, -4, 5].filter(isPositive); // number[]Constructs a type which equals to narrowed type in predicate function Type.
type PredicateType<Type> = Type extends (arg: any) => arg is infer U ? U : never;Usage Example:
import type { PredicateType } from "ts-essentials";
// Extract the narrowed type from a predicate function
const isStringArray = (value: unknown): value is string[] => {
return Array.isArray(value) && value.every(item => typeof item === "string");
};
type StringArrayType = PredicateType<typeof isStringArray>; // string[]
// Use with other predicates
const isUser = (value: unknown): value is { id: number; name: string } => {
return typeof value === "object" &&
value !== null &&
"id" in value &&
"name" in value;
};
type UserType = PredicateType<typeof isUser>; // { id: number; name: string }
// Generic utility for working with predicate types
function processWithPredicate<P extends PredicateFunction>(
value: unknown,
predicate: P,
processor: (value: PredicateType<P>) => void
): void {
if (predicate(value)) {
processor(value); // Type is properly narrowed
}
}
// Usage
processWithPredicate(
someValue,
isUser,
(user) => {
console.log(user.id, user.name); // Type-safe access
}
);Combine function types for complex operations:
import type { AnyFunction, PredicateFunction, PredicateType } from "ts-essentials";
// Function composition utilities
type ComposableFunction<T> = AnyFunction<[T], T>;
function compose<T>(...fns: ComposableFunction<T>[]): ComposableFunction<T> {
return (value: T) => fns.reduce((acc, fn) => fn(acc), value);
}
// Pipeline with type predicates
type Pipeline<T> = {
filter<U extends T>(predicate: (value: T) => value is U): Pipeline<U>;
map<U>(mapper: AnyFunction<[T], U>): Pipeline<U>;
collect(): T[];
};
function createPipeline<T>(items: T[]): Pipeline<T> {
return {
filter<U extends T>(predicate: (value: T) => value is U): Pipeline<U> {
return createPipeline(items.filter(predicate));
},
map<U>(mapper: AnyFunction<[T], U>): Pipeline<U> {
return createPipeline(items.map(mapper));
},
collect(): T[] {
return items;
}
};
}
// Usage
const data = [1, "hello", 2, "world", 3] as const;
const result = createPipeline(data)
.filter((x): x is string => typeof x === "string")
.map((s: string) => s.toUpperCase())
.collect(); // string[]
// Advanced predicate combinations
type CombinedPredicate<T, U, V> = (
value: T
) => value is U & V;
function createCombinedPredicate<T, U, V>(
predicate1: (value: T) => value is U,
predicate2: (value: T) => value is V
): CombinedPredicate<T, U, V> {
return (value: T): value is U & V => {
return predicate1(value) && predicate2(value);
};
}
// Function signature validation
type ValidateFunction<F extends AnyFunction> = {
argCount: Parameters<F>['length'];
returnType: ReturnType<F>;
isValid: boolean;
};
function validateFunction<F extends AnyFunction>(
fn: F,
expectedArgCount: number
): ValidateFunction<F> {
return {
argCount: fn.length as Parameters<F>['length'],
returnType: undefined as ReturnType<F>, // Conceptual
isValid: fn.length === expectedArgCount
};
}type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;Install with Tessl CLI
npx tessl i tessl/npm-ts-essentials