remeda 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
\n

remeda Library for TypeScript: Complete Guide

\n

In the world of modern web development with TypeScript, data processing is one of the most common tasks. The remeda library (formerly known as Remeda) is a modern, type-safe alternative to Lodash, specifically designed for TypeScript. In this article, we will thoroughly explore what remeda is, how to install it, what methods it provides, and how to use it in real projects.

\n\n

1. What It Is and Why You Need It

\n

Remeda is a utility library for functional programming in TypeScript. Unlike Lodash, which was originally written for JavaScript and only later received types, remeda was designed from the start with strict typing in mind. This means that all functions in remeda are fully type-safe: the TypeScript compiler can infer precise types for input and output data, significantly reducing the number of runtime errors.

\n

Why do you need remeda? First, it provides convenient functions for working with arrays, objects, strings, and other data structures that are frequently used in everyday development. Second, remeda encourages a functional programming style: most functions are pure, do not mutate the original data, and are easily composable using pipes. Third, remeda has a very small size (tree-shakeable), allowing only the functions that are actually used to be included in the bundle.

\n

Key advantages of remeda over Lodash: full TypeScript support "out of the box", immutability by default, support for pipes for function composition, and a more modern API aligned with ES6+ standards.

\n\n

2. Installation

\n

Remeda is distributed via npm. To install, run the command:

\n
npm install remeda
\n

Or, if you use yarn:

\n
yarn add remeda
\n

After installation, you can import functions individually (recommended for tree-shaking) or import the entire module:

\n
// Import individual functions (recommended)\nimport { pipe, map, filter } from 'remeda';\n\n// Import the entire module\nimport * as R from 'remeda';
\n\n

3. Quick Start — Minimal Working Example

\n

Let's write a simple example: we have an array of numbers, we want to filter even numbers, then multiply each by 2, and output the result.

\n
import { pipe, filter, map } from 'remeda';\n\nconst numbers = [1, 2, 3, 4, 5, 6];\n\nconst result = pipe(\n    numbers,\n    filter(x => x % 2 === 0), // [2, 4, 6]\n    map(x => x * 2)           // [4, 8, 12]\n);\n\nconsole.log(result); // [4, 8, 12]
\n

As you can see, the code reads like a sequence of operations: "take numbers, filter even ones, then multiply by 2". This is the essence of the functional approach that remeda promotes.

\n\n

4. Main Methods/Functions

\n

Let's look at 8 key functions of remeda. For each, we provide the signature, a brief description, and an example.

\n\n

4.1. pipe

\n

Signature: pipe<A, B, C, ...>(value: A, ...fns: Function[]): LastReturnType

\n

What it does: Takes an initial value and a list of functions. Sequentially applies each function to the result of the previous one. This is the primary way to compose functions in remeda.

\n
import { pipe } from 'remeda';\n\nconst addOne = (x: number) => x + 1;\nconst double = (x: number) => x * 2;\n\nconst result = pipe(5, addOne, double); // (5 + 1) * 2 = 12\nconsole.log(result); // 12
\n\n

4.2. map

\n

Signature: map<T, R>(fn: (value: T, index: number,

Recommendations