TypeScript Reference ยท tsconfig: Language, Modules and Emit Options
TypeScript types and typeRoots options
Reference for the types and typeRoots options: which @types packages become global, how to limit them, and the error you get when a global is missing.
Some packages describe globals instead of exports: @types/node adds process and Buffer, a test-runner package adds describe and it. The types option chooses which of those packages are loaded into every file of the project. typeRoots chooses the folders in which such packages are searched.
Older compilers included every package under node_modules/@types automatically. TypeScript 6 changed the default of types to an empty list, so you now name the packages you want. Check your version; listing them explicitly works everywhere and keeps the global scope small.
{ "compilerOptions": { "types": ["node", "vitest/globals"], "typeRoots": ["./node_modules/@types", "./types"] } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
{ "compilerOptions": { "types": ["<package>"], "typeRoots": ["<folder>"] } }
| Option | Description |
|---|---|
types |
Array of package names (resolved inside the type roots) whose global declarations are included. [] includes none. A name may include a path, such as "vitest/globals". |
typeRoots |
Array of folders searched for those packages. Default: node_modules/@types in the project folder and every parent folder. |
Edge cases: types only controls automatic global inclusion. An explicit import of a package, or a /// <reference types="..." /> directive, always loads that package's types even when it is not listed. A package named in types that cannot be found is an error (TS2688, "Cannot find type definition file").
Usage notes
- Browser projects should not list
node, otherwiseprocessand Node'ssetTimeouttyping leak into front-end code. Use"types": []plus thelibentries you need. - Test globals (
describe,it,expect) come from@types/jest,@types/mochaor theglobalsentry of your runner. Add them in a separatetsconfig.test.jsonso production code cannot use them. - Missing global. The message for an unknown global such as
describeoften hints at the package to install:Do you need to install type definitions for a test runner? typeRootsis rarely needed. Use it for a local folder of hand-written globals, for example./types/legacy-lib/index.d.ts.- Version differences. After upgrading, a project that relied on automatic inclusion may suddenly miss
process; add"types": ["node"]. - Speed. Fewer included packages means fewer files to parse in every check.