What is @types/underscore?
@types/underscore provides TypeScript type definitions for the Underscore.js library, which is a utility-belt library for JavaScript that provides a lot of the functional programming support you would expect in Prototype.js (or Ruby), but without extending any of the built-in JavaScript objects.
What are @types/underscore's main functionalities?
Array Operations
Underscore provides a variety of functions to manipulate arrays, such as map, filter, reduce, and more. In this example, the map function is used to double each number in the array.
const _ = require('underscore');
const numbers = [1, 2, 3, 4, 5];
const doubled = _.map(numbers, (num) => num * 2);
console.log(doubled); // [2, 4, 6, 8, 10]
Object Operations
Underscore offers several functions to work with objects, such as keys, values, extend, and more. This example demonstrates the keys function, which retrieves all the keys of an object.
const _ = require('underscore');
const person = { name: 'John', age: 30, job: 'Developer' };
const keys = _.keys(person);
console.log(keys); // ['name', 'age', 'job']
Utility Functions
Underscore includes various utility functions like identity, constant, noop, and more. The every function checks if all elements in an array pass a given predicate function.
const _ = require('underscore');
const isEven = (num) => num % 2 === 0;
const result = _.every([2, 4, 6], isEven);
console.log(result); // true
Function Operations
Underscore provides functions to manipulate other functions, such as bind, debounce, throttle, and more. The once function ensures a function is only called once.
const _ = require('underscore');
const greet = (name) => `Hello, ${name}!`;
const greetOnce = _.once(greet);
console.log(greetOnce('John')); // 'Hello, John!'
console.log(greetOnce('Jane')); // undefined
Other packages similar to @types/underscore
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It is similar to Underscore but offers more features and better performance.
ramda
Ramda is a practical functional library for JavaScript programmers. It focuses on immutability and side-effect-free functions, providing a more functional approach compared to Underscore.
lazy.js
Lazy.js is a utility library similar to Underscore and Lodash but with a lazy evaluation approach, which can lead to performance improvements in certain scenarios.
Installation
npm install --save @types/underscore
Summary
This package contains type definitions for underscore (https://underscorejs.org/).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/underscore.
Additional Details
- Last updated: Sat, 19 Oct 2024 03:38:51 GMT
- Dependencies: none
Credits
These definitions were written by Boris Yankov, Josh Baldwin, Christopher Currens, Ard Timmerman, Julian Gonggrijp, Florian Imdahl, Regev Brody, Piotr Błażejewicz, Michael Ness, and Luke Tsekouras.