ts-functional-pipe for TypeScript: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

ts-functional-pipe for TypeScript: Complete Guide with Examples

1. What It Is and Why You Need It

ts-functional-pipe is a lightweight TypeScript library that provides a powerful, type-safe pipe operator and a set of utilities for functional composition. Unlike the proposed |> operator in JavaScript (which is not yet standardized), this library lets you use an elegant, readable syntax for sequential data processing today. The core idea is to replace nested function calls (f(g(h(x)))) with a linear chain (pipe(x, h, g, f)), dramatically improving code readability and maintainability.

Why do you need it? In modern TypeScript development, we often perform multiple sequential transformations on data: filtering, mapping, sorting, validation, and transformation. Without a pipe, such code turns into a "pyramid" of nested functions or a chain of mutable variables. ts-functional-pipe solves this by providing a declarative way to describe data flow. The library is fully typed, meaning TypeScript checks types at every stage of the pipe, catching errors at compile time.

This library is especially useful in projects using a functional approach: array processing, working with Observables (RxJS), transforming API responses, building middleware, and more. It doesn't impose any paradigms—it simply gives you a convenient tool for function composition. ts-functional-pipe is more than just a pipe; it's a full set of helper functions that make functional programming in TypeScript comfortable and safe.

2. Installation

Install the library using your standard package manager (npm, yarn, or pnpm). The library is written in TypeScript and ships with ready-made type declaration files, so no additional @types installation is needed.

# Install via npm
npm install ts-functional-pipe

# Install via yarn
yarn add ts-functional-pipe

# Install via pnpm
pnpm add ts-functional-pipe

After installation, import the functions you need into your project:

import { pipe, compose, map, filter, reduce } from 'ts-functional-pipe';

3. Quick Start — Minimal Working Example

Let's start with a simple example: transform a number by applying several operations. Without the library, this would look like Math.sqrt(Math.abs(-25)). With ts-functional-pipe, we can write it linearly:

import { pipe } from 'ts-functional-pipe';

// Define simple functions
const addOne = (x: number): number => x + 1;
const double = (x: number): number => x * 2;
const toString = (x: number): string => `Result: ${x}`;

// Use pipe for sequential application
const result = pipe(
    5,          // Initial value
    addOne,     // 5 + 1 = 6
    double,     // 6 * 2 = 12
    toString    // "Result: 12"
);

console.log(result); // Output: "Result: 12"

As you can see, the code reads from top to bottom, which is much more intuitive than nested calls. pipe automatically passes the result of the previous function as the argument to the next one.

4. Core Methods/Functions

The library provides a rich set of functions. Let's explore 8 key ones.

4.1. pipe

Recommendations