how to use the every function from the underscore library in typescript

To use the every function from the Underscore library in TypeScript, you first need to install the library and its type definitions.

index.ts
npm install underscore
npm install --save-dev @types/underscore
64 chars
3 lines

Once you have installed Underscore and its type definitions, you can import the library and use the every function as follows:

index.ts
import * as _ from 'underscore';

const isValid = _.every([1, 2, 3, 4], (num) => {
  return num > 0;
});

console.log(isValid); //true
135 chars
8 lines

The every function takes an array as its first argument and a predicate function as its second argument. It returns true if all items in the array pass the predicate function, false otherwise.

In the example above, the every function checks if all numbers in the array are greater than 0. Since all numbers in the array meet this condition, the function returns true.

gistlibby LogSnag