🌐
npm
npmjs.com › package › @types › lodash
@types/lodash - npm
1 month ago - TypeScript definitions for lodash. Latest version: 4.17.24, last published: a month ago. Start using @types/lodash in your project by running `npm i @types/lodash`. There are 11413 other projects in the npm registry using @types/lodash.
      » npm install @types/lodash
    
Discussions

How to import lodash in a typescript library package?
I'd like to centralize my generic most-used (typescript) functions in a Util package which I can reuse across my projects. Turned out more difficult than expected. This package is not going to be More on stackoverflow.com
🌐 stackoverflow.com
A Typescript-first alternative to Lodash/Underscore
Check out ‘radash’ on npm. It advertises itself as a ‘modern lodash’ lol. It has almost everything lodash has, written with first-class TS. Unfortunately, something to keep in mind is that the maintainer of the project hasn’t merged any pull requests or fixed any issues in the last couple of months. Which couldd be a problem based on your needs. More on reddit.com
🌐 r/typescript
41
42
January 20, 2023
ts node - How to use lodash-es in typescript correctly? - Stack Overflow
I need to use lodash-es in my typescript project, but I can't configure it correctly, it always reports errors like SyntaxError: Unexpected identifier hello.ts import upperCase from 'lodash-es/ More on stackoverflow.com
🌐 stackoverflow.com
Importing lodash into angular2 + typescript application
I am having a hard time trying to get the lodash modules imported. I've setup my project using npm+gulp, and keep hitting the same wall. I've tried the regular lodash, but also lodash-es. The lodas... More on stackoverflow.com
🌐 stackoverflow.com
JavaScript library in the functional programming paradigm
Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm. Lodash is a fork of Underscore.js. It joined the Dojo Foundation in 2013, and … Wikipedia
Factsheet
Lodash°
Original author John-David Dalton
Initial release April 23, 2012; 13 years ago (2012-04-23)
Factsheet
Lodash°
Original author John-David Dalton
Initial release April 23, 2012; 13 years ago (2012-04-23)
🌐
Lodash
lodash.com
Lodash
Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.
Top answer
1 of 1
1

I found two three possible working scenarios:

  1. Use TSdx, the Zero-config CLI for TypeScript package development. https://tsdx.io/
  2. Use lodash-es instead (but note that chains are not working in lodash-es!). Explanation below. This however still gives Jest errors when this library is imported in another library, and tested there.
  3. Use vitest instead. Vitest is largely compatible with the Jest api. After installing Vitest, everything simply worked, without any configuration. The only thing to do is to add import { assert, expect, test } from 'vitest' to your tests. If you have just started, and only have simple tests, they simply worked without any changes (YMMV). In addition, both lodash and lodash-es worked fine.

A better answer would include a scenario that uses "plain" lodash, or explains why lodash-es is actually a good thing. From what I'm reading in various posts is that lodash-es has several drawbacks (results in a larger bundle size, chains don't work).

Here is a working setup with (I think) sensible defaults. This library is for internal use, and I run Node 18, so I use a high target version here.

package.json

{
  "name": "@vexna/util",
  "version": "1.0.0",
  "description": "Generic utilities, uses lodash",
  "private": true,
  "type": "module",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "sideEffects": false,
  "scripts": {
    "build": "rimraf dist && tsc && rollup -c rollup.config.js",
    "test": "node test/spec",
    "pretest": "npm run build"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@rollup/plugin-typescript": "^11.0.0",
    "@types/lodash-es": "^4.17.6",
    "lodash-es": "^4.17.21",
    "rimraf": "^4.1.2",
    "rollup": "^3.12.1",
    "typescript": "^4.9.5"
  },
  "files": [
    "dist"
  ],
  "peerDependencies": {
    "lodash": "^4.17.21"
  }
}

tsconfig.ts

{
    "compilerOptions": {
      "module": "es2022",
      "moduleResolution": "node",
      "outDir": "dist",
      "declaration": true,
      "sourceMap": true,
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "forceConsistentCasingInFileNames": true,
      "lib": ["es2022", "DOM", "DOM.Iterable"],
      "target": "es2022",
      "skipLibCheck": true,
      "strict": true,
      "exactOptionalPropertyTypes": true,
      "noImplicitAny": true,
      "noImplicitThis": true,
      "checkJs": true
    },
    "include": ["./src/**/*.ts"]
}

rollup.config.js

import typescript from '@rollup/plugin-typescript'
const input = ["src/index.ts"]

export default [
    {
        input,
        plugins: [
            typescript()],
        output: [
            {
                dir: "dist",
                sourcemap: true,
            }
        ],
        external: ['lodash-es'],
    }
]

The output has lodash entirely externalized, so very small bundle! As a precaution, I added "lodash": "^4.17.21" to the "peerDependencies". This is a little odd cause it's a mismatch with devDependencies (which uses the -es), but this seems fine. YMMV.

In the library code, you can import the lodash functions like so:

import { now, random, padStart } from "lodash-es"

A project that consumes this library must have lodash, and can import like so:

import { createUid } from '@vexna/util'
🌐
DEV Community
dev.to › oooopsitsme › why-you-should-use-lodash-es-in-your-typescript-projects-153n
Why You Should Use `lodash-es` in Your TypeScript Projects - DEV Community
August 23, 2024 - Lodash has excellent TypeScript support, with types available out of the box. This makes working with lodash-es a breeze when using TypeScript.
🌐
Medium
sanjanahumanintech.medium.com › lodash-library-usage-in-javascript-typescript-cafc82818dd5
Lodash Library usage in Javascript/typescript | by Sanjana Human In Tech | Medium
November 26, 2023 - Lodash is a popular utility library for JavaScript that provides a wide range of helpful functions to make working with arrays, objects, strings, and other data types more convenient and efficient.
🌐
Reddit
reddit.com › r/typescript › a typescript-first alternative to lodash/underscore
r/typescript on Reddit: A Typescript-first alternative to Lodash/Underscore
January 20, 2023 -

EDIT: I forgot to put a question mark at the end of the title. Whoops!

I understand that both libraries have "@types/..." that can easily be downloaded. Still, some corners of the library would have been better designed if they were initially built in Typescript rather than tacking on a.d.ts file later.

I am starting a new project and would like to know if any alternatives have been developed recently.

🌐
My Memory
putridparrot.com › blog › using-lodash-in-typescript
Using lodash in TypeScript | My Memory
May 12, 2019 - Was trying to use lodash and couldn’t seem to get Visual Code to build my TypeScript files correctly, so here’s how to get it working…
Find elsewhere
🌐
Total TypeScript
totaltypescript.com › workshops › advanced-typescript-patterns › external-libraries › navigating-lodash-s-type-definitions
Navigating Lodash's Type Definitions | Total TypeScript
June 8, 2023 - 0:00 In the next exercise, the one after this, we're going to be diving into the types of lodash. I want to give you some tools to help you navigate this library. Now, this is the main way that you import from lodash. You can also do things like lodash groupBy, or things like this, or grab individual functions from it.
Top answer
1 of 16
508

Here is how to do this as of Typescript 2.0: (tsd and typings are being deprecated in favor of the following):

$ npm install --save lodash

# This is the new bit here: 
$ npm install --save-dev @types/lodash

Then, in your .ts file:

Either:

import * as _ from "lodash";

Or (as suggested by @Naitik):

import _ from "lodash";

I'm not positive what the difference is. We use and prefer the first syntax. However, some report that the first syntax doesn't work for them, and someone else has commented that the latter syntax is incompatible with lazy loaded webpack modules. YMMV.

Edit on Feb 27th, 2017:

According to @Koert below, import * as _ from "lodash"; is the only working syntax as of Typescript 2.2.1, lodash 4.17.4, and @types/lodash 4.14.53. He says that the other suggested import syntax gives the error "has no default export".

2 of 16
70

Update September 26, 2016:

As @Taytay's answer says, instead of the 'typings' installations that we used a few months ago, we can now use:

npm install --save @types/lodash

Here are some additional references supporting that answer:

  • https://www.npmjs.com/package/@types/lodash
  • TypeScript typings in NPM @types org packages

If still using the typings installation, see the comments below (by others) regarding '''--ambient''' and '''--global'''.

Also, in the new Quick Start, config is no longer in index.html; it's now in systemjs.config.ts (if using SystemJS).

Original Answer:

This worked on my mac (after installing Angular 2 as per Quick Start):

sudo npm install typings --global
npm install lodash --save 
typings install lodash --ambient --save

You will find various files affected, e.g.

  • /typings/main.d.ts
  • /typings.json
  • /package.json

Angular 2 Quickstart uses System.js, so I added 'map' to the config in index.html as follows:

System.config({
    packages: {
      app: {
        format: 'register',
        defaultExtension: 'js'
      }
    },
    map: {
      lodash: 'node_modules/lodash/lodash.js'
    }
  });

Then in my .ts code I was able to do:

import _ from 'lodash';

console.log('lodash version:', _.VERSION);

Edits from mid-2016:

As @tibbus mentions, in some contexts, you need:

import * as _ from 'lodash';

If starting from angular2-seed, and if you don't want to import every time, you can skip the map and import steps and just uncomment the lodash line in tools/config/project.config.ts.

To get my tests working with lodash, I also had to add a line to the files array in karma.conf.js:

'node_modules/lodash/lodash.js',
🌐
DEV Community
dev.to › tipsy_dev › advanced-typescript-reinventing-lodash-get-4fhe
Advanced TypeScript: reinventing lodash.get - DEV Community
September 5, 2021 - Also, despite having three generic type parameters, we don't need to provide any types there. As all generics are mapped to function parameters, TypeScript infers these upon the function call from the actual values. Let's revisit our component. In the initial implementation, we used lodash.get which didn't raise an error for a mismatched type.
🌐
npm
npmjs.com › package › @types › lodash-es
@types/lodash-es - npm
November 21, 2023 - TypeScript definitions for lodash-es. Latest version: 4.17.12, last published: 2 years ago. Start using @types/lodash-es in your project by running `npm i @types/lodash-es`. There are 1521 other projects in the npm registry using @types/lodash-es.
      » npm install @types/lodash-es
    
🌐
GitHub
github.com › DefinitelyTyped › DefinitelyTyped › tree › master › types › lodash
DefinitelyTyped/types/lodash at master · DefinitelyTyped/DefinitelyTyped
The repository for high quality TypeScript type definitions. - DefinitelyTyped/types/lodash at master · DefinitelyTyped/DefinitelyTyped
Author   DefinitelyTyped
🌐
Ben Ilegbodu
benmvp.com › blog › understanding-typescript-generics-lodash-functions
Understanding TypeScript generics through lodash functions | Ben Ilegbodu
August 22, 2021 - It's used as the default callback function in many of the lodash functions. It takes any type of value and returns back that value. So if we pass in a string value, we get back that same string value. Or if we pass in some object type, we get back that same object of the same type. So how do we use TypeScript to type it?
🌐
egghead.io
egghead.io › lessons › typescript-using-lodash-in-typescript-with-typings-and-systemjs
Using Lodash in TypeScript with Typings and SystemJS | egghead.io
One of the most confusing parts of getting started with TypeScript is figuring out how to use all the libraries that you know and love from JavaScript. This lesson walks you through including Lodash in your project, installing Lodash definition files, and then properly loading Lodash with SystemJS.
Published   April 21, 2016
🌐
Delft Stack
delftstack.com › home › howto › typescript › lodash typescript
How to Use Lodash in TypeScript | Delft Stack
February 2, 2024 - Lodash is a JavaScript library that provides utility functions for our everyday programming tasks. These include getting the first or last array element, a specific array element based on the position, chunking or slicing an array, etc.
🌐
Bennadel
bennadel.com › blog › 3328-fixing-lodash-typescript-errors-by-upgrading-types-lodash-in-node-or-webpack.htm
Fixing Lodash TypeScript Errors By Upgrading @types/lodash In Node Or Webpack
April 21, 2020 - Ben Nadel shares a set of TypeScript error messages that he was getting when attempting to create a proxy method around Lodash. The fix was to upgrade TypeScript and the Definitely Typed "@types/lodas
🌐
StackShare
stackshare.io › stackups › lodash-vs-typescript
TypeScript vs Lodash | What are the differences? | StackShare
Lodash - A JavaScript utility library delivering consistency, modularity, performance, & extras. It provides utility functions for common programming tasks using the functional programming paradigm.