Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Language, Modules and Emit Options

TypeScript allowJs option

Reference for allowJs: including .js files in a TypeScript project, how they are emitted and typed, and the step-by-step migration path with checkJs.

allowJs lets .js, .jsx, .mjs and .cjs files be part of a TypeScript project. They can be imported from TypeScript files, they are copied or transpiled to the output folder together with your .ts files, and the compiler uses them to infer types for the things they export.

It is the starting point of a gradual migration. You turn it on, rename files one by one from .js to .ts, and keep the program working all the time. By itself the option adds only syntax checks to JavaScript files; to get type errors in them, add checkJs or put // @ts-check at the top of single files.

JSON
{
  "compilerOptions": {
    "allowJs": true,
    "outDir": "dist"
  },
  "include": ["src"]
}

Try it

TypeScript Importing a file the project does not contain

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.

TypeScript Typing the boundary to old code

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

JSON
{ "compilerOptions": { "allowJs": true } }
Part Description
Value true or false. Default: false.
Files Extensions .js, .jsx, .mjs, .cjs that match include/files.
Emit JavaScript files are transpiled (to the target and module you chose) into outDir, or checked only when noEmit is set.
Types Inferred from the code and from JSDoc comments.
Declarations With declaration or emitDeclarationOnly, .d.ts files are generated for JavaScript files too (TypeScript 3.7 and later).

Edge cases: a JavaScript file that would be emitted over its own source is an error (TS5055, "Cannot write file ... because it would overwrite input file"); set outDir. Files in node_modules are normally not included; see maxNodeModuleJsDepth.

Usage notes

  • Without checkJs you get almost no new errors, only syntax problems and a few structural ones. That is intended: first make the build work, then add checking.
  • Inference from JavaScript patterns. Property assignments in a constructor (this.x = 1) and module.exports = ... are understood. JSDoc @param and @type tags supply real types.
  • Transpiler bonus. With allowJs the same pipeline can down-level your JavaScript files, which is handy when a project must support an older target.
  • Order of migration. Convert leaf modules first (no imports from other unconverted files), so their types flow outward.
  • Editors. allowJs also makes IntelliSense in JavaScript use TypeScript's inference even when you never run tsc (a jsconfig.json is a tsconfig with allowJs on by default).
  • Do not combine with a build that lacks outDir for source files and output in the same folder.