how to use the words function from the lodash library in typescript

To use the words() function from the Lodash library in TypeScript, you should first install the Lodash library and add its types to your project.

You can install Lodash using npm:

npm install --save lodash
26 chars
2 lines

You can then import the words() function from Lodash and use it in your TypeScript code like this:

index.ts
import { words } from 'lodash';

const sentence = 'Lorem ipsum dolor sit amet';
const wordsArray = words(sentence);

console.log(wordsArray); // ["Lorem", "ipsum", "dolor", "sit", "amet"]
188 chars
7 lines

Note that the words() function splits a string into an array of words, separated by whitespace. You can also pass a pattern as the second argument to split the string by a different separator. The typings for lodash should be available automatically if you're using TypeScript >= 2.6.0.

gistlibby LogSnag