Radash Utility Library
Radash is a modern, functional utility library for JavaScript and TypeScript, providing a comprehensive collection of helper functions with zero external dependencies. It's designed to be simple, powerful, and fully typed, offering alternatives to functions found in older libraries like Lodash while focusing on a smaller bundle size and native ESM support. The library is actively maintained, currently on version 12.1.1, with frequent minor and patch releases. Key differentiators include its strong, native TypeScript typing, which aims to improve type safety compared to libraries that often require extra type packages, and a modular design that facilitates tree-shaking for optimized bundle sizes. Radash focuses on functions that augment modern JavaScript features, intentionally omitting those already well-covered by the language itself, leading to a cleaner and more intuitive API.
Common errors
-
TypeError: _.someFunction is not a function
cause Attempting to use a Radash function that is not part of the `_` namespace or is not a named export when using `import * as _ from 'radash'` or `import { someFunction } from 'radash'` respectively. It could also mean the function name is incorrect or it's a function from a different library (e.g., Lodash) that Radash does not provide.fixVerify the function name against the Radash documentation. If using `import * as _ from 'radash'`, ensure the function is accessed via `_.functionName`. If using named imports, ensure `functionName` is directly exported. For CommonJS, `const { functionName } = require('radash')` is typical. -
TS2307: Cannot find module 'radash' or its corresponding type declarations.
cause TypeScript cannot locate the module or its type definitions. This typically occurs due to incorrect installation, misconfigured `tsconfig.json` paths, or using an outdated TypeScript version that doesn't properly resolve modern `package.json` 'exports' maps.fixEnsure `radash` is correctly installed (`npm install radash` or `yarn add radash`). Verify your `tsconfig.json` includes appropriate `moduleResolution` (e.g., 'NodeNext' or 'Bundler') and `module` (e.g., 'ESNext') settings for modern package resolution. Ensure TypeScript is up-to-date.
Warnings
- breaking The `sum` function received a 'type safer implementation' in v12.0.0. While intended to improve type inference and correctness, this change could lead to TypeScript errors if previous usage was less strictly typed or relied on implicit coercions.
- gotcha Radash intentionally avoids polymorphic behavior in some functions (e.g., `_.map` expecting only arrays, not objects) unlike older libraries like Lodash. This design choice promotes stronger type safety and deterministic behavior but means a direct drop-in replacement for highly polymorphic Lodash functions might require code adaptation.
- gotcha When migrating from older utility libraries, be aware that Radash omits many functions that have become obsolete due to modern JavaScript features (e.g., optional chaining, null coalescing, native `Array.prototype` methods). Attempting to use these non-existent functions will result in runtime errors.
- deprecated Prior to v10.6.0, Radash was licensed under BSD 3-Clause. As of v10.6.0, the license changed to MIT. While not a breaking functional change, developers relying on the specific terms of the BSD license should be aware of this update.
Install
-
npm install radash -
yarn add radash -
pnpm add radash
Imports
- * as _
import * as _ from 'radash'
- { max, sum }
import { max, sum } from 'radash/dist/esm'import { max, sum } from 'radash' - CommonJS require
const _ = require('radash')const { get } = require('radash')
Quickstart
import * as _ from 'radash'
const gods = [{
name: 'Ra',
power: 'sun',
rank: 100,
culture: 'egypt'
}, {
name: 'Loki',
power: 'tricks',
rank: 72,
culture: 'norse'
}, {
name: 'Zeus',
power: 'lightning',
rank: 96,
culture: 'greek'
}]
// Basic array operations
console.log('Max rank god:', _.max(gods, g => g.rank));
console.log('Total rank sum:', _.sum(gods, g => g.rank));
// Object manipulation
const godObject = _.objectify(
gods,
g => g.name.toLowerCase(),
g => _.pick(g, ['power', 'rank', 'culture'])
);
console.log('Gods as object:', godObject);
// Asynchronous operations with error handling
const mockApi = {
gods: {
findByName: async (name) => {
if (name === 'loki') throw new Error('Loki is elusive');
return gods.find(g => g.name.toLowerCase() === name);
}
}
};
async function fetchAndLogGod(name) {
const [err, god] = await _.try(mockApi.gods.findByName)(name);
if (err) {
console.error(`Failed to fetch ${name}:`, err.message);
} else {
console.log(`Fetched ${name}:`, god);
}
}
fetchAndLogGod('ra');
fetchAndLogGod('loki');