Chapter 1 · Practice 3 of 6
Set types and rootDir explicitly
Why it matters
TypeScript 6.0 changed two defaults that surprise people on upgrade, and TypeScript 7.0 keeps them:
typesnow defaults to[]. Global type packages under@types, such as@types/nodeor a test runner's globals, are no longer included automatically. List the ones you need, for example"types": ["node"]. Setting["*"]restores the old behavior, but an explicit list is clearer and faster.rootDirnow defaults to the directory that containstsconfig.json. If your source lives insrc/and you emit todist/, setrootDirto./srcor your output folder will gain an extrasrc/level.
Being explicit about both also stops a stray @types package, pulled in by some dependency, from adding globals your code should not see.
Do
- List global type packages in
types, such as["node"]or["node", "vitest/globals"] - Set
rootDirandoutDirtogether when you emit JavaScript
Avoid
- Relying on every
@typespackage innode_modulesbeing picked up automatically
Example
{
"compilerOptions": {
"types": ["node"],
"rootDir": "./src",
"outDir": "./dist"
},
"include": ["src"]
}