Anton Telesh
Anton Telesh

Reputation: 3822

DefinitelyTyped Lodash

I want to import single function from lodash while preserving typings. Such as this:

import invokeMap from 'lodash/invokeMap';

But it seems that I cannot do so. Compiler says that there is no module lodash/invokeMap. It's correct — DefinitelyTyped declares only top-level module for lodash, so I can import only the whole package. But I want to import only one function.

Is there a way to import only typings information about lodash without importing lodash itself?

Below is my workaround plan. I want to use standard CommonJS require function to import the necessary function without types information and then cast type onto it:

declare var require;

var invokeMap: _.invokeMap = require('lodash/invokeMap');
               ^^^^^^^^^^^
               Here I want to cast proper type from DefinitelyTyped,
               but how to reach that `invokeMap` typings alone?

Upvotes: 5

Views: 2594

Answers (3)

Muhammad Fawwaz Orabi
Muhammad Fawwaz Orabi

Reputation: 853

As of TypeScript 2, you can install typings directly from npm:

npm install @types/lodash

Those typings have declarations for each individual module.

Upvotes: 3

svi3c
svi3c

Reputation: 575

With the current typings lodash this should work as you tried it before:

import invokeMap from 'lodash/invokeMap';

just install a current version of the ambient dependencies like so:

typings i -SA github:DefinitelyTyped/DefinitelyTyped/lodash/lodash.d.ts#672afd8f7b09d839b61eeb4867753876cf79c8b6

Upvotes: 2

toskv
toskv

Reputation: 31600

You can use object destructuring to import only the invokeMap function from the module.

import {invokeMap} from 'lodash';

invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');

Upvotes: 1

Related Questions