Skip to main content

Help! My Types Are Missing!

TypeScript Node does not use files, include or exclude, by default. This is because a large majority projects do not use all of the files in a project directory (e.g. Gulpfile.ts, runtime vs tests) and parsing every file for types slows startup time. Instead, ts-node starts with the script file (e.g. ts-node index.ts) and TypeScript resolves dependencies based on imports and references.

For global definitions, you can use the typeRoots compiler option. This requires that your type definitions be structured as type packages (not loose TypeScript definition files). More details on how this works can be found in the TypeScript Handbook.

Example tsconfig.json:

{
"compilerOptions": {
"typeRoots" : ["./node_modules/@types", "./typings"]
}
}

Example project structure:

<project_root>/
-- tsconfig.json
-- typings/
-- <module_name>/
-- index.d.ts

Example module declaration file:

declare module '<module_name>' {
// module definitions go here
}

For module definitions, you can use paths:

{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"custom-module-type": ["types/custom-module-type"]
}
}
}

An alternative approach for definitions of third-party libraries are triple-slash directives. This may be helpful if you prefer not to change your TypeScript compilerOptions or structure your custom type definitions when using typeRoots. Below is an example of the triple-slash directive as a relative path within your project:

/// <reference types="./types/untyped_js_lib" />
import UntypedJsLib from "untyped_js_lib"

Tip: If you must use files, include, or exclude, enable --files flags or set TS_NODE_FILES=true.