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

To use the keys function from the underscore library in Typescript, you need to first install the underscore library using npm:

index.ts
npm install underscore --save
30 chars
2 lines

Then, you need to install the type definitions for underscore:

index.ts
npm install @types/underscore --save-dev
41 chars
2 lines

With the underscore library and its type definitions installed, you can import and use the keys function in your Typescript code:

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

const myObject = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
};

const keys = _.keys(myObject);
console.log(keys); // ['key1', 'key2', 'key3']
188 chars
11 lines

The keys function takes an object as its argument and returns an array of the object's own enumerable property names, in the same order as the for...in loop would give.

gistlibby LogSnag