mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
Notes for lodash developers
Folder Structure
- Root
index.d.ts: this is the main file that will be imported when people doimport _ from "lodash". It references files for the lodash types.fp.d.ts: likeindex.d.ts, but for the functional programming variant of lodash. See https://github.com/lodash/lodash/wiki/FP-Guide.lodash-tests.ts: contains test cases. Update these as necessary when you make a change.tsconfig.json: usually you shouldn't modify this file. However, if you add a new file, you should probably add it to thefileslist intsconfig.json.tslint.json: contains lint rules. The goal is to remove all of the rule overrides and match only thedtslint/dt.jsonrules.- All other files: these exist so people can import individual functions, e.g.
import * as flatMap from "lodash/flatMap"
commondirectory: contains the main lodash types, split into multiple files for maintainability reasons. These files are NOT meant to be imported directly - they should only be referenced byindex.d.ts.fpdirectory: contains individual functions forlodash/fp. These files may be imported. You should not modify these scripts directly - you should use a script to re-generate them (see below).scriptsdirectory: contains code generation scripts.- Before running any scripts, run
npm installin this directory (it contains its ownpackage.json). - Most notable script is
npm run generate, which re-generates all of thefpfiles andlowdbwrapper extensions.
- Before running any scripts, run
v3directory: contains types for lodash v3.
Different ways people might use lodash
- Importing
import * as _ from "lodash"import * as _ from "lodash/fp"import { flatMap } from "lodash"import { flatMap } from "lodash/fp"import * as flatMap from "lodash/flatMap"import * as flatMap from "lodash/fp/flatMap"import * as flatMap from "lodash.flatmap"(requiresnpm install lodash.flatmap)import _ = require("lodash")import _ from "lodash"(only ifesModuleInteropis enabled)
- Global namespace
Before creating a PR
- For every function you modify, don't forget to update the corresponding wrapper functions.
- Re-generate the
fptypes by opening a terminal in thescriptsdirectory and runningnpm run generate.- Note that this directory has its own
package.json, so you'll need to runnpm installfirst if you haven't already.
- Note that this directory has its own
- Back at the root directory, do
npm run lint lodashand make sure there are no errors.
FAQ
- I'm fixing a bug in v4. Should I also update the v3 types?
- In general, no.
- If the wrapper functions are almost copies of the original functions, shouldn't we auto-generate them like we do for
lodash/fp?- Good idea! If you have time, submit a PR.
- When I ran
npm run lint lodash, I got an error that looks like<--- Last few GCs --->.- Yeah, this error is really annoying. It means that node.js ran out of memory before it could run all of your tests.
- If you see something like
Test with 2.6before that error, it means that there's an error in an older version of typescript. The hard part is figuring out what the error is.
- If you see something like
- The general procedure for diagnosing these errors is:
- Delete half of the tests in
lodash-tests.ts(either the top half or the bottom half).
- If you delete the top half, don't delete the important stuff like
interface AbcObject.
- Run
npm run lint lodash. - If it succeeds, add that half back and delete the other half.
- If it fails with a GC error, delete half of the remaining tests.
- Note: If both halves succeed on their own, then the tests are probably just consuming too much memory. Try simplifying them until they pass.
- Repeat steps 1-4 until it gives you the real error message. Usually it's something obscure that only happens in TS 2.3/T.4, so commenting/modifying the test is usually the best solution.
- Delete half of the tests in
- Yeah, this error is really annoying. It means that node.js ran out of memory before it could run all of your tests.