A ready-made tsconfig.json: strict, and the flags that keep TypeScript in sync with Node
Move TaskKit’s long check line into a tsconfig.json, so plain tsc checks every file, tests included. Learn what strict switches on, and the three settings that make the check refuse what Node refuses: a type in a plain import, an enum, and an import path without .ts.
TypeScript
Fundamentals
A ready-made `tsconfig.json`: `strict`, and the flags that keep TypeScript in sync with Node. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
Open TaskKit's package.json and read the check line:
tsc --noEmit--types node --allowImportingTsExtensions index.ts.
Every flag on it came from a problem you had to solve, and it still
misses things. Lesson 9.1's type in a plain import passed this check,
and then Node stopped. So did lesson 7.6's enum, and so does an
import path without .ts. The check and Node follow different rules,
and you find out from Node.
Tell a cook the rules out loud every time, "no salt, oven at 180
degrees, twenty minutes", and one day half of them get forgotten. So
the rules go on a card pinned to the fridge, written once and read
every time. By the end of this lesson TaskKit has that card for its
check: a settings file. The check becomes plain tsc (the TypeScript
compiler, the checker). It checks every file of the project, tests
included, and it refuses what Node would refuse.
A settings file for the check
tsconfig.json is the file tsc looks for in the folder it
runs in, and in the folders above it. It is written in the same
format as package.json, called JSON (JavaScript Object
Notation, a way of writing data down as text): names in double
quotes, a colon, the value, a comma between lines. Everything sits
under one name, compilerOptions: the settings of the check,
one per line.
In VS Code (Visual Studio Code, your editor), make a new file in the
taskkit folder, name it tsconfig.json, and paste this ready-made
file into it:
true turns a setting on, and ["node"] is an array with one name
in it, written as in TypeScript (lesson 4.1). The rest of this lesson
takes the seven lines in three groups. Now the check needs no flags
and no file name. In package.json, change the check script:
package.jsonjson
"check": "tsc"
Here are two separate commands, npm run check and then npm start,
and who reads the new file in each (npm is short for Node package
manager). The first five arrows are npm run check, the last two
are npm start. Press Next step to walk through it, or let it
play:
npm
tsc
tsconfig.json
Node
1
npm to tsc: tsc
2
tsc to tsconfig.json: readthesettings
3
tsconfig.json to tsc: strict,noEmit,types…
4
tsc, to itself: checkeveryfile
5
tsc to npm: noerrors
6
npm to Node: nodeindex.ts
7
Node, to itself: striptypesandrun
Of npm, tsc and Node, only tsc reads tsconfig.json
All 7 steps shown.Step 1 of 7: npm → tsc — tscStep 2 of 7: tsc → tsconfig.json — read the settingsStep 3 of 7: tsconfig.json → tsc — strict, noEmit, types …Step 4 of 7: tsc — check every fileStep 5 of 7: tsc → npm — no errorsStep 6 of 7: npm → Node — node index.tsStep 7 of 7: Node — strip types and run
All 7 steps shown.
One thing changes now that the file name is gone. With a file name,
tsc checked that file and the files it imports, so tasks.test.ts
and the other test files were never part of npm run check. With no
file name, tsc checks every .ts file in the folder and the
folders under it, tests included, and leaves node_modules alone.
Run the check:
bash
npm run check
Outputtext
> check> tsc
Nothing after the > lines: the check passed. If it names a file
that neither TaskKit nor a test imports, it is practice code from an
earlier lesson, now checked too: fix it or delete it.
Node's documentation
says it does not read tsconfig.json, so npm start and npm test
run exactly as before.
VS Code does read it: the TypeScript inside the editor uses the same
file. So the red line VS Code has kept under process since lesson
2.8 is gone. Hover over word in command.ts: it now shows
const word: string | undefined, the type the check works out, where
it said any before.
The old flags, now lines
Three lines are the flags of the old check line, moved into the
file. They do what they did:
"noEmit": true only checks: it emits (writes out) no .js
copies (lesson 1.4).
"types": ["node"] loads Node's description, for process
(lesson 2.8).
"allowImportingTsExtensions": true accepts .ts in import paths
(lesson 5.11).
Keep noEmit: allowImportingTsExtensions works only beside it.
Without it, tsc reports an error and still writes a .js copy
beside every .ts file, which you would then have to delete.
strict: one switch for many checks
Many of the errors you have fixed come from one group of checks, the
strict checks:
'…' is possibly 'undefined', TS18048 (TS for TypeScript, then
the number of the message), since lesson 3.3;
Parameter '…' implicitly has an 'any' type, TS7006, since
lesson 5.1;
'error' is of type 'unknown' in a catch, since lesson 8.3.
strict is the switch for that group: one line that turns on the
checks catching a box that may be empty, a type nobody wrote, a caught
error used before anyone checked what it is. TypeScript 6 and 7 turn
it on by themselves, which is why you have had it all along.
TypeScript 5 and older did not: under them, a project with no strict
line had every strict check off, and some projects say "strict": false outright. Written out, your file says which way it is, to you
and to anyone who opens it.
Here is what the switch is worth. Make a file of practice code in
the taskkit folder:
Bad — copied from an older project,
it lets the empty box through to Node.
tsconfig.jsonjson
"strict": false,
Good — the check stops it, as you
are used to.
tsconfig.jsonjson
"strict": true,
To see the difference, set strict to false, run npm run check,
then node done.ts. The check passes, and Node stops on line 3:
The line that matterstext
TypeError: Cannot read properties of undefined (reading 'title')
No task is done, so find gave back undefined. Set strict back
to true, and the check says it first:
What the check printstext
done.ts:3:28 - error TS18048: 'firstDone' is possibly 'undefined'.3 console.log(`First done: ${firstDone.title}`); ~~~~~~~~~Found 1 error in done.ts:3
Every strict check is switched off by that one word, and nothing
tells you. Keep true, and delete done.ts before you go on.
Three rules Node follows, now in the check
The last three lines are why the file was worth writing. Each one
makes the check follow a rule Node follows when it runs your code,
so a mistake Node would stop on is stopped by the check first.
verbatimModuleSyntax makes the check refuse a type in a plain
import. Verbatim means word for word: the check reads each import
line as Node will. Node blanks out only the lines import type
marks (lesson 9.1), so a type left in a plain import is refused
here first.
erasableSyntaxOnly makes the check refuse anything Node
cannot simply strip out, such as an enum (lesson 7.6). Erasable
means it can be erased and leave plain JavaScript behind.
"module": "nodenext" tells TypeScript to use Node's rules,
the newest version of them, to find files: the whole file name,
.ts included. And, like Node, it reads "type": "module" in
package.json, so it treats every file as a module (lesson 9.1).
Switch between the three cases, and press Next step to walk
through each, or let it play. In each, Node starts only if you run
it, and the check has told you first:
npm run check runs plain tsc, which loads the settings
file first.
"module": "nodenext": Node never adds .ts to a path. The
old check line let it through.
The check stopped first. Run it anyway, and Node says Cannot find module for command.
With tsconfig.json, the check stops what Node would stop
All 3 steps shown.Step 1 of 3: TypeScript: reads tsconfig.jsonStep 2 of 3: TypeScript: TS2835Step 3 of 3: Node: never starts
All 3 steps shown.
Try the first one in TaskKit. In print.ts, replace the two import
lines from ./tasks.ts with this one, as lesson 9.1's mistake did:
print.tsts
import { alphabetical, percentFull, Task } from "./tasks.ts";
npm run check prints its two > lines, and then this; the first
line is one long line in your terminal, broken here to fit:
What the check printstext
print.ts:3:37 - error TS1484: 'Task' is a type and must be importedusing a type-only import when 'verbatimModuleSyntax' is enabled.3 import { alphabetical, percentFull, Task } from "./tasks.ts"; ~~~~Found 1 error in print.ts:3
"Type-only import" is import type. Put the two lines back, and the
check is clean again. Next, the enum. Make lesson 7.6's status.ts
again in the taskkit folder:
status.tsts
enum Status { Todo = "todo", Done = "done",}const mine: Status = Status.Done;console.log(mine);
Run npm run check. Before Node ever sees the file, the check
refuses its first line:
What the check printstext
status.ts:1:6 - error TS1294: This syntax is not allowed when'erasableSyntaxOnly' is enabled.1 enum Status { ~~~~~~Found 1 error in status.ts:1
Delete status.ts again. Last, the path. In index.ts, take .ts
off the import of readCommand, so the line ends with
from "./command";. Then run npm run check:
What the check printstext
index.ts:4:29 - error TS2835: Relative import paths need explicitfile extensions in ECMAScript imports when '--moduleResolution' is'node16' or 'nodenext'. Did you mean './command.js'?4 import { readCommand } from "./command"; ~~~~~~~~~~~Found 1 error in index.ts:4
A relative import path is one that starts with ./, or ../
for the folder above: a file of yours. ECMAScript is the official
name of the JavaScript language, and ES, as in ES modules, is short
for it. So ECMAScript imports are the import lines of ES modules
(lesson 9.1). moduleResolution is the setting for how files are
found, and "module": "nodenext" sets it to nodenext for you.
node16 is an older version of the same rules.
Put .ts back, and npm run check prints only its two > lines.
Predict the check
A friend adds a test to tasks.test.ts, with the number of tasks
in quotes by mistake. npm test passes it: "3" divided by 5 still
comes to 60%. With the old check line, npm run check said nothing.
Predict
Which error code does npm run check print now?
tasks.test.tsts
test("3 tasks of 5, from a text box", () => { assert.equal(percentFull("3", 5), 60);});
The common mistake: a file name after tsc
Since Module 1 you have checked a file by naming it. The habit stays
after the old check line is gone.
Common mistake
The old way, with tsconfig.json in the folder
You check one file the way you always did, with npx, which starts
the project's tsc:
bash
npx tsc --noEmit index.ts
tsc checks nothing and answers with one long line, broken here to
fit:
Outputtext
error TS5112: tsconfig.json is present but will not be loaded iffiles are specified on commandline. Use '--ignoreConfig' to skipthis error.
error TS5112: tsconfig.json is present but will not be loaded if files are specified on commandline. Use '--ignoreConfig' to skip this error.
The fix
Leave the file name out, and tsc reads the settings and checks the
whole project:
bash
npm run check
A file name tells tsc to forget tsconfig.json, and a check without
its settings is the old check again, so tsc refuses rather than
quietly checking the wrong way. --ignoreConfig is not the fix: it
forgets the file, types, allowImportingTsExtensions and the three
rules included, so TaskKit is refused again for process (no Node
description) and for the .ts at the end of its plain import lines.
npx tsc alone does the same as npm run check.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
These run in your browser, so they ask about the check rather than
run it. Try any of them in TaskKit for real.
Exercise · Read the code
Check it with its settings
You type npx tsc --noEmit tasks.ts in TaskKit and get
error TS5112: tsconfig.json is present but will not be loaded if files are specified on commandline. Which command checks TaskKit
with its settings?
Exercise · Fix the error
Not the file the message suggests
This program prints an import line from index.ts. The check said
TS2835 … Did you mean './command.js'?, and someone took the
advice: now the check passes, but Node stops with
Cannot find module. Fix it, so the program prints the line Node
can run.
Loading the editor…
Exercise · Predict the result
With strict switched off
A friend's project has "strict": false in its tsconfig.json.
Its file holds this function, with no type on the parameter:
ts
function shout(word) { return word.toUpperCase();}
How many errors does npx tsc print for these three lines?
Exercise · Write the code
From flags to lines
The old check line had three flags: --noEmit, --types node and
--allowImportingTsExtensions. Write a program that prints the
three lines of tsconfig.json that took their place, in that order,
one per line, each as it stands in the file with its comma.
Loading the editor…
Recap
Recap · 3 questions
Check yourself
Who reads tsconfig.json?
Check yourself
With "strict": false, what happens to firstDone.title?
Check yourself
Why do verbatimModuleSyntax and erasableSyntaxOnly matter?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
A ready-made tsconfig.json
What things are
Word
What it means
tsconfig.json
Read by tsc and VS Code; Node never reads it
JSON
JavaScript Object Notation, the format of the two .json files
compilerOptions
The settings of the check, one per line
strict
One switch for the group of strict checks
Relative import path
Starts with ./ or ../: a file of yours
Verbatim
Word for word: import lines taken as written
Erasable
Can be stripped out, leaving plain JavaScript
The settings
Line
To
"strict": true
Turn on possibly 'undefined', TS7006 and the rest
"noEmit": true
Only check; write no .js copies
"types": ["node"]
Load Node's description, for process
"module": "nodenext"
Find files as Node does: the whole name
"allowImportingTsExtensions": true
Accept .ts in import paths
"verbatimModuleSyntax": true
Refuse a type in a plain import: TS1484
"erasableSyntaxOnly": true
Refuse what Node cannot strip: TS1294
Rules
Rule
What it means
"check": "tsc"
No flags and no file name
Every file
Plain tsc checks tests too, not node_modules
No file name
A file name gives TS5112
TS2835
Write ./command.ts, not the .js it suggests
Write strict out
Older TypeScript had it off
Module 9 on one page
Cheat sheet
Module 9: Modules, Packages, and Projects
import and export: "type": "module", .ts extensions,
import type
Write
To
export const …
Share a box; only exported names leave a file
import { capitalize }
Bring in a value, by its exact name
import type { Task }
Bring in a type; Node blanks the line
from "./tasks.ts"
A file of yours: the whole file name
npm pkg set type=module
Tell Node, once, how the files are written
npm and package.json: installing packages and writing scripts
Write
To
npm install boxen
Add a dependency
npm install --save-dev prettier
Add a dev dependency
npm run check
Run the script called check
npm test, npm start
Run those two; no run needed
from "boxen"
A bare name: a package in node_modules
Types for packages: @types and the types setting
Word or code
What it means
.d.ts
A description: types only, no code
npm install --save-dev @types/pluralize
Adds a description
An icon on npmjs.com
A hint only: the check decides
TS7016
No description anywhere: look for an @types package
--types node
Loads Node's description, for process
A ready-made tsconfig.json: strict, and the flags that keep
TypeScript in sync with Node
Write
To
tsconfig.json
Keep the check's settings in one file
"strict": true
Keep every strict check on
"types": ["node"]
Load Node's description, from the file
verbatimModuleSyntax, erasableSyntaxOnly
Refuse what Node refuses
"module": "nodenext"
Find files as Node does
"check": "tsc"
Check every file, tests included
Where to go next
You can now give a project a settings file, say what each of its
seven lines does, and read the three errors it adds as Node's rules
showing up early. TaskKit's check is plain tsc, it reads every
file, tests included, and it stops what Node would stop on.
Next is What <T> means: Array<T> and Map<K, V>, the start
of Module 10. You have written Task[] and read Map since Module 6;
there you learn to read the <T> that types like these carry, and
you will meet it in the descriptions of the packages you install.
Before you move on, run npm run check, npm test and
npm start add "pay rent" one after another, and see all three pass.
Then open tsconfig.json and say out loud, line by line, what each
one does.