lodash: ready-made helpers, with types from @types/lodash
See why a ready-made helper beats four hand-written sorting rules, install lodash and its description from @types/lodash, read which lodash version that description was written for, import lodash whole the way Node can load it, and sort TaskKit’s list by due date with _.sortBy and keys TypeScript checks, so the late task comes first and a task with no date comes last.
Fundamentals
TypeScript
`lodash`: ready-made helpers, with types from `@types/lodash`. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
It is Tuesday 27 October 2026, and TaskKit's list is full. You read it
from the top:
Outputtext
Your list, A to Z:#5 [todo] Book dentist, due: Fri 30 Oct, in 3 days#3 [todo] Buy milk, due: not set#2 [todo] Call Mom, due: Tue 27 Oct, today#1 [todo] Pay rent, due: Fri 30 Oct, in 3 days#4 [todo] Water the plants, due: Sun 25 Oct, 2 days late
The plants have waited two days, and they are on the last line,
because W comes last in the alphabet. Since lesson 14.5 TaskKit knows
every due date, so it could put the list in the order you have to do
things: the earliest date first, a task with no date at the end, and
two tasks on the same day from A to Z. Writing that order yourself
means a compare function with four rules, and every one of them has
to be right.
A kitchen drawer has a garlic press in it. You could chop garlic with
a knife; the press does it in one squeeze, the same way every time.
In this lesson TaskKit gets a drawer of ready-made helpers, a package
called lodash. By the end, the list is sorted by due date with one
call, TypeScript checks that call through a second package,
@types/lodash, and you know the two mistakes that check cannot see.
Why a helper
lodash is a package of about 300 small functions for lists and
objects: sort a list by several things, remove repeats, split a list
into groups of three, and so on. Every one of them hangs on a single
object, which everyone names _: the underscore, a "low dash",
which is where the package got its name. Many of its helpers are older than
the same idea in JavaScript itself, and lodash is still downloaded
more than a hundred million times a week.
The one TaskKit needs is _.sortBy. Here is the order the cold
open asked for, written both ways. Both give the same order for the
same tasks:
print.ts
tasks.toSorted((a, b) => { if (a.due === b.due) { return alphabetical( a.title, b.title, ); } if (a.due === undefined) { return 1; } if (b.due === undefined) { return -1; } return alphabetical( a.due, b.due, );});
By hand, with toSorted
Does: Works with nothing installed
Never: Never handles a missing date or a tie unless you write it
With lodash
Does: Sorts by the first key, then the second; no date goes last
Never: Never runs where lodash is not installed
One order: four rules you write and get right, or two keys
The first card is a compare function, as in lesson 4.4, with
TaskKit's alphabetical doing the comparing. The second card only
says what to sort by. lodash has already worked out ties and missing
dates, and tested them, for everyone who uses it.
Install lodash and its description
In lesson 14.1 you saw that lodash brings no types: its
package.json has no "types" line and its folder holds no .d.ts
file, so the check would refuse the first import with TS7016, as it
refused pluralize in lesson 9.3. The description comes from @types,
as a dev dependency (dev for development: a package you need while you
write and check the code, not to run it). In the taskkit folder, run:
The first command prints:
The second prints the same, with audited 25 packages; your times, in
milliseconds (ms), will differ. package.json gains
"lodash": "^4.18.1" under dependencies and
"@types/lodash": "^4.17.27" under devDependencies. @types/lodash
copies lodash's first two numbers, so its 4.17 says it describes
lodash 4.17. The code you just installed is 4.18. lodash 4.18 added and
removed no functions, so the description still fits, but nobody
promised that. Volunteers update
@types/lodash when they can; lodash's own authors do not publish
it. That is the gap lesson 14.1 meant when it called built-in types
the better sign: they change together with the code. Your versions
may be newer.
One import, two packages
So lodash reaches TaskKit as two packages, written by different
people. Every import of it is read twice: TypeScript reads it against
@types/lodash, and Node runs it against lodash's own code, a file
called lodash.js. Most of the time the two agree. Switch between the
two import lines, and press Next step to follow each, or let it
play:
As with import { Task } in lesson 9.1, the check passes and Node
stops, but for a new reason: the check reads one package and Node
runs another. The description says lodash has a sortBy, which is
true. It does not say how Node can load it. lodash is written the
CommonJS way, Node's older way of sharing code (lesson 9.1), and a
CommonJS package hands over one object and nothing else.
Picking one thing out of a package by its own name, as
import { sortBy } asks, takes a named export. date-fns and
commander have named exports; lodash has none. import _ takes the
default export instead (lesson 9.2), under a name you choose, as
you did with boxen and pluralize: here that is lodash's one object,
and you call each helper on it, _.sortBy. The common mistake below
shows what Node prints for the other line.
Try the import that works. Save this as try.ts in taskkit and run
it with node try.ts:
Given only a list, _.sortBy sorts its values. Dates written year,
month, day sort as text into the right order, which is one more
reason TaskKit keeps them that way (lesson 14.5). Like toSorted,
it gives back a new list and leaves dates as it was.
Two kinds of key
Tasks are objects, so _.sortBy has to be told what to sort them by.
Each thing it sorts by is a key, and they go in a list, the most
important first. Replace try.ts with this:
Each key here is a function, written as in lesson 4.3: it gets one
task and gives back the value to sort by. _.sortBy sorts by the
first key, the due date, so Tuesday comes before Friday. Only where
the first key gives the same value, two tasks on Friday, does the
second key decide: Book dentist before Pay rent. A task whose key
gives undefined, Buy milk with no date, goes last. Tasks that
every key leaves equal stay in the order they came.
You never wrote a type for task, and yet TypeScript knows it is a
Task. The description is generic, as in Module 10: it is written
sortBy<T>, takes a list of T and gives back T[]. Hover sortBy
in VS Code (Visual Studio Code, your editor) and it says
sortBy<Task>(…) and, after the brackets, : Task[]: this call fills
T with Task, as findBy did in lesson 10.2. So each key gets a
Task, and sorted is a Task[].
A key can also be the name of a property, in quotes: "due" and
"title" sort the same way. The description lets any text through
there, though, and that is the second mistake the check cannot see, after
import { sortBy }.
Bad — the keys are text, and the check
accepts any text, so "titel" sorts Friday's two tasks as they came.
Good — the keys are functions,
which TypeScript checks against Task.
The text version runs, prints, and is wrong only for two tasks on the
same day, which is the kind of mistake you find a week later. lodash
looks for a property called titel, finds none on any task, and so
has nothing to sort them by. Make the same typo in the function,
(task) => task.titel, and run npm run check. It stops the file
before anything runs, and suggests the right name. The error's first line is
one long line in your terminal, broken here to fit:
TaskKit sorts by due date
lodash and its description are installed, and you know which import
and which keys to write. Now TaskKit gets them, in print.ts, where
the list is printed. Delete try.ts first.
1
Sort in print.ts
Make lodash's import the first line of print.ts, above boxen's:
printList no longer needs alphabetical. Change the import from
./tasks.ts:
At the end of the file, beside dueText, add the function that
sorts:
Then replace the start of printList, from the toSorted call to
the for line:
byDueDate prints nothing and only decides an order, so a test can
call it, as the tests of lesson 14.5 call dueText.
2
Test the order
In print.test.ts, import byDueDate beside dueText:
Then add two tests at the end of the file:
3
Check, run, and test
To see the order at work, give TaskKit the list from the start of this
lesson. Delete tasks.json, then:
The check prints only its two > lines. Under the banner, the last
add prints:
These outputs are from a run on Tuesday 27 October 2026. Your days left
will differ, counted from your own today; the order follows the dates.
The tests end with ℹ pass 18 and ℹ fail 0: the
16 from before, and the two new ones.
Predict before you run
Today is Tuesday 27 October. Your list holds three tasks: Call Mom,
due today, Pay rent, due on Friday, and Buy milk, with no date.
You add one more for today:
Predict
Which task does the list print first?
The common mistake: one helper by name
After date-fns, import { format } from "date-fns" is a habit, so the
same shape for lodash feels right, and the check agrees with it.
Common mistake
Importing one lodash helper by name
print.ts picks sortBy out of lodash on its first line, and
byDueDate calls it without _.:
npm run check prints only its two > lines. Then
npm start add "buy bread" prints no banner and no list, only Node's
error:
Node calls it a SyntaxError although the line is written correctly:
it is Node's name for an import it cannot make. The SyntaxError line
is one long line in your terminal, broken here to fit. It is lesson
9.1's message,
but sortBy is no type: lodash is a CommonJS package, and Node finds
no name in the one object it hands over, module.exports in
CommonJS's words. Node stops while it loads the files, before
index.ts runs a single line; after the error come the stack trace
and the Node.js line. Put the same import in the file you run, and
Node says more: Named export 'sortBy' not found. The requested module 'lodash' is a CommonJS module, with the advice to import the whole
package, which is the fix below.
SyntaxError: The requested module 'lodash' does not provide an export named 'sortBy'
The fix
Import the whole package, and call the helper on it:
When Node says a package "does not provide an export named" a helper
its description has, the package is CommonJS: import it whole. The
check could not warn you, because @types/lodash describes lodash's
helpers, not how Node loads them.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
lodash is not installed on this page, so the exercises ask about
lodash and its description, and sort by hand where code has to run.
Recap
Recap · 3 questions
Check yourself
You wrote no type for task in the keys. Why is it a Task?
Check yourself
Why are TaskKit's keys functions, not names in quotes?
Check yourself
Where does _.sortBy put a task whose key gives undefined?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
lodash: ready-made helpers, with types from @types/lodash
What things are
Word
What it means
lodash
About 300 small helpers for lists and objects
_
The name everyone gives lodash's one object
@types/lodash
lodash's description, by volunteers, no code
Key
What _.sortBy sorts by: a function or a property's name
Named export
One thing a package shares under its own name
Code
Write
What you get
npm install lodash
lodash in dependencies
npm install --save-dev @types/lodash
Its description, a dev dependency
import _ from "lodash"
The whole package, as _
_.sortBy(list)
A new list, its values sorted
_.sortBy(tasks, [key, key])
Sorted by the first key, ties by the next
(task) => task.due
A key the check reads against Task
Rules
Rule
What it means
does not provide an export named, a helper
CommonJS: import _
Keys as functions
A name in quotes is never checked
No value goes last
A task with no date ends the list
Read both versions
@types/lodash 4.17 describes lodash 4.17
Module 14 on one page
Cheat sheet
Module 14: Popular Libraries, Step One
Why libraries exist, and how to choose one
Ask
Where to look
Does it do the job?
The readme
Do many projects use it?
Weekly Downloads
Is anyone looking after it?
Last publish, and what the readme says
Where are its types?
"types" in its package.json, else @types
Staying safe: lockfiles, pinned versions, and what a supply-chain
attack is
Word or command
What it means
Lockfile
package-lock.json: what npm installed, exactly
npm ci
Empty node_modules, install exactly the lock
--save-exact
Pin it: npm install --save-exact boxen
Supply-chain attack
An attack on something your program trusts
chalk: colors in the terminal
Write or rule
What it means
chalk.green(text)
A new string, green in a terminal
chalk.red.bold(text)
A colour and a style from one call
Colour where you print
Values you save, compare and test stay plain
commander: a real command-line interface
Write
What it does
.command("add")
Where to go next
You can now install a package and its description from @types, read
which version that description was written for, import lodash the way
Node can load it, and sort TaskKit's list by more than one key with
keys TypeScript checks.
Next is Finish TaskKit: add a feature that fetches data from an API,
and write a README, the first lesson of Module 15. Here an API (application
programming interface) is a website that answers programs, as in
lesson 12.4. There TaskKit gets its last feature and a page that tells
other people what it does.
Before you move on, try a second helper. _.countBy counts how many
items give each key. Make try.ts again with the four tasks from
"Two kinds of key", and print
_.countBy(tasks, (task) => task.due ?? "no date"). Before you run
it, hover countBy in VS Code: after the brackets it says
_.Dictionary<number>, lodash's name for an object whose values are
numbers. Guess the
object, run it, then delete try.ts.
Yourcode
TypeScript
Node
Screen
Your code: print.tsprint.tsprint.ts
TypeScript: checked ✓checked✓checked✓
Node: runs ✓runs✓runs✓
Screen: Your list…Yourlist…Yourlist…
Time
The first line reads import _ from "lodash";.
TypeScript reads the description in @types/lodash. It
describes sortBy, so the check passes.
Node loads lodash's code, lodash.js, which hands over one
object. _ is that object, with sortBy inside.
TaskKit runs and prints the list, sorted by _.sortBy.
TypeScript checks the import against @types/lodash; Node
runs it against lodash
All 4 steps shown.Step 1 of 4: Your code: print.tsStep 2 of 4: TypeScript: checked ✓Step 3 of 4: Node: runs ✓Step 4 of 4: Screen: Your list…
All 4 steps shown.
Yourcode
TypeScript
Node
Screen
Your code: print.tsprint.tsprint.ts
TypeScript: checked ✓checked✓checked✓
Node: No export named 'sortBy'Noexportnamed'sortBy'Noexportnamed'sortBy'
The first line reads import { sortBy } from "lodash";.
TypeScript reads the description in @types/lodash. It
describes sortBy, so the check passes.
Node loads the same lodash.js. It cannot take sortBy out of
lodash's one object by name, so it stops with a SyntaxError
before a single line of TaskKit runs.
Not one line of TaskKit ran, though the check said yes.
TypeScript checks the import against @types/lodash; Node
runs it against lodash
All 4 steps shown.Step 1 of 4: Your code: print.tsStep 2 of 4: TypeScript: checked ✓Step 3 of 4: Node: No export named 'sortBy'Step 4 of 4: Screen: nothing printed
All 4 steps shown.
A new command; the calls after it describe it
.option("--due <date>")
An option that needs a value
.action(…)
Run your function with the arguments, then the options
await program.parseAsync()
Read the line, check it, run the action
date-fns: working with due dates
Write
What you get
parseISO("2026-10-30")
A Date, midnight on your computer
isValid(date)
true if it holds a real moment
format(due, "EEE d MMM")
Fri 30 Oct
differenceInCalendarDays
How often the date changes; below 0 if late
lodash: ready-made helpers, with types from @types/lodash
Write or rule
What it means
npm install --save-dev @types/lodash
Its description, a dev dependency
import _ from "lodash"
The whole package, as _
_.sortBy(tasks, [key, key])
Sorted by the first key, ties by the next
Keys as functions
A name in quotes is never checked
Exercise · Read the code
What the check lets through
Which of these lines passes npm run check and then stops Node
before TaskKit prints anything?
Exercise · Predict the result
Ties keep their order
tasks holds Pay rent (due 30 October), then Book dentist (due 30
October), then Call Mom (due 27 October), in that order. After this
line, which title is second in sorted?
> check> tsctry.ts:12:18 - error TS2551: Property 'titel' does not exist on type'Task'. Did you mean 'title'?12 (task) => task.titel, ~~~~~ tasks.ts:4:3 - 'title' is declared here. 4 title: string; ~~~~~Found 1 error in try.ts:12
Outputtext
Added: Book dentist5 tasks added, room for 0 moreList 100% fullYour list, by due date:#4 [todo] Water the plants, due: Sun 25 Oct, 2 days late#2 [todo] Call Mom, due: Tue 27 Oct, today#5 [todo] Book dentist, due: Fri 30 Oct, in 3 days#1 [todo] Pay rent, due: Fri 30 Oct, in 3 days#3 [todo] Buy milk, due: not set
export function printList(tasks: Task[]): void { const byName = tasks.toSorted((a, b) => alphabetical(a.title, b.title) ); console.log("Your list, A to Z:"); for (const task of byName) { console.log("Your list, by due date:"); for (const task of byDueDate(tasks)) {
print.test.tsts
import { dueText } from "./print.ts";import { byDueDate, dueText } from "./print.ts";
print.test.tsts
test("byDueDate puts the late task first and no date last", () => { const sorted = byDueDate([ { id: 1, title: "Buy milk", status: "todo" }, { id: 2, title: "Pay rent", due: "2026-10-30", status: "todo" }, { id: 3, title: "Water plants", due: "2026-10-25", status: "todo" }, ]); assert.equal(sorted[0].title, "Water plants"); assert.equal(sorted[2].title, "Buy milk");});test("byDueDate puts the tasks of one day A to Z", () => { const sorted = byDueDate([ { id: 1, title: "Pay rent", due: "2026-10-30", status: "todo" }, { id: 2, title: "Book dentist", due: "2026-10-30", status: "todo" }, ]); assert.equal(sorted[0].title, "Book dentist");});
file:///Users/ana/projects/taskkit/print.ts:1import { sortBy } from "lodash"; ^^^^^^SyntaxError: The requested module 'lodash' does not provide anexport named 'sortBy'
file:///C:/Users/ana/projects/taskkit/print.ts:1import { sortBy } from "lodash"; ^^^^^^SyntaxError: The requested module 'lodash' does not provide anexport named 'sortBy'
file:///home/ana/projects/taskkit/print.ts:1import { sortBy } from "lodash"; ^^^^^^SyntaxError: The requested module 'lodash' does not provide anexport named 'sortBy'
Exercise · Fix the error
A second key, by hand
This program should print the tasks by due date and, on the same day,
from A to Z: Call Mom, Book dentist, Pay rent. It prints
Pay rent before Book dentist. Change the compare function so the
title decides when the two dates are the same.