allowJs Option
In this page:
Core Concept
The allowJs compiler option lets tsc include plain .js files in the same compilation as your .ts files, which is the option that makes incremental JavaScript-to-TypeScript migration possible in the first place.
Example: Core Concept
// tsconfig.json: { "compilerOptions": { "allowJs": true } }
console.log("allowJs lets tsc include plain .js files in the same compilation");
Basic Setup
Enable it with "allowJs": true in tsconfig.json; by default this only allows .js files to be imported and compiled, without type-checking their contents.
Example: Basic Setup
// By default, allowJs only allows importing .js -- it doesn't type-check it.
console.log("allowJs alone compiles .js files without checking their contents");
Typed Example
A typed example: with allowJs on, a .ts file can import { helper } from './utils.js' and TypeScript will infer types for helper from its usage, even though utils.js itself has no type annotations.
Example: Typed Example
// utils.js (no types)
// export function helper(x) { return x * 2; }
// main.ts
// import { helper } from "./utils.js"; // TypeScript infers from usage
function helper(x: number) { return x * 2; }
console.log(helper(5));
Project Usage
In a real project, allowJs is the very first flag turned on at the start of a JS-to-TS migration, letting the codebase compile as a mixed .js/.ts project throughout the transition instead of requiring an atomic rewrite.
Example: Project Usage
// allowJs is the very first flag turned on at the start of a JS-to-TS migration.
console.log("Lets a mixed .js/.ts codebase compile throughout the transition");
Best Practices
Pair allowJs with checkJs only once you're ready to start catching type errors inside the .js files themselves — leaving checkJs off keeps migration noise low in the early stages.
Example: Best Practices
// tsconfig.json: { "allowJs": true, "checkJs": false } (initially)
console.log("Add checkJs only once ready to catch type errors inside .js files");
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: