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

First, you need to install the Underscore library and type definitions via npm:

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

In your Typescript file, import Underscore and the shuffle function:

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

const myArray = [1, 2, 3, 4, 5];
const shuffledArray = _.shuffle(myArray);
console.log(shuffledArray); // Random order of numbers from 1 to 5
176 chars
6 lines

Make sure to include the type definitions in your tsconfig.json file:

{
  "compilerOptions": {
    ...
    "types": [
      "underscore"
    ]
  }
}
79 chars
9 lines

You can also specify the types directly in your Typescript file:

index.ts
import { shuffle } from 'underscore';

const myArray: number[] = [1, 2, 3, 4, 5];
const shuffledArray = shuffle(myArray);
console.log(shuffledArray); // Random order of numbers from 1 to 5
189 chars
6 lines

With this, you can now use the shuffle function from the Underscore library in your Typescript project.

gistlibby LogSnag