Lodash Dictionary replacement in ES6

Viewed 2993

We've been using lodash in an old TypeScript project. Now we're migrating to ES6 modules so replacing lodash with lodash-es. In lodash there is a Dictionary type declared here: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/common/common.d.ts#L247-L249

interface Dictionary<T> {
    [index: string]: T;
}

We used it to declare some fields like this:

labTransGrouped: Dictionary<Array<ILabtransItem>>;

There is no obvious replacement in lodash-es. I assume I should use something like TypeScript Map instead. But what is the right way to use it in type declaration in my case? I could not find any mention of that in lodash changelog or migration guides, so must be something simple.

2 Answers

I faced the same problem.

lodash-es certainly does not have Dictionary type, but Dictionary is such a simple type you don't even need to create by yourself.

Simply copy the Dictionary type somewhere in your code and import.

export interface Dictionary<T> {
  [index: string]: T;
}

lodash-es uses definitions from lodash and also Dictionary.

import _ from 'lodash-es';
import { Dictionary } from 'lodash';

interface ILabtransItem {
    a: string;
}

const arr: Dictionary<Array<ILabtransItem>> = {}
const groupedArr = _.groupBy(arr, x => x[0].a)

Try it out here
See the definition of groupBy for lodash-es here

Related