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.
{ "compilerOptions": { "allowJs": true, "outDir": "dist" }, "include": ["src"] }
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": { "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
checkJsyou 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) andmodule.exports = ...are understood. JSDoc@paramand@typetags supply real types. - Transpiler bonus. With
allowJsthe same pipeline can down-level your JavaScript files, which is handy when a project must support an oldertarget. - Order of migration. Convert leaf modules first (no imports from other unconverted files), so their types flow outward.
- Editors.
allowJsalso makes IntelliSense in JavaScript use TypeScript's inference even when you never runtsc(ajsconfig.jsonis a tsconfig withallowJson by default). - Do not combine with a build that lacks
outDirfor source files and output in the same folder.