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

To use the _.has() function from the Underscore library in TypeScript, you need to first import the library and its types:

index.ts
import * as _ from 'underscore';
import { Dictionary } from 'underscore';

const obj: Dictionary<number> = { a: 1, b: 2, c: 3 };
129 chars
5 lines

Then, you can use the _.has() function to check if an object has a certain key:

index.ts
if (_.has(obj, 'a')) {
  console.log('Object has key "a"');
}
62 chars
4 lines

Alternatively, you can use the in operator in TypeScript to achieve the same result:

index.ts
if ('a' in obj) {
  console.log('Object has key "a"');
}
57 chars
4 lines

Both of these snippets will output "Object has key "a"" if the object contains the key "a".

gistlibby LogSnag