← Back to TypeScript Course | Chapter 27: Migration from JavaScript | Lesson 2 of 6

allowJs Option

The allowJs compiler option permits JavaScript files to be included in a TypeScript project. It is useful during migration from JavaScript to TypeScript.
Syntax
typescript
{
  "compilerOptions": {
    "allowJs": true
  }
}

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

typescript
// 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

typescript
// 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

typescript
// 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

typescript
// 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

typescript
// tsconfig.json: { "allowJs": true, "checkJs": false }  (initially)
console.log("Add checkJs only once ready to catch type errors inside .js files");
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Turning on allowJs and expecting the .js files to be type-checked, when they are only compiled unless checkJs is on.
  2. Setting allowJs without an outDir, so the output .js overwrites the original source files.
  3. Importing a .js file with no types and not realizing all its exports come in as loosely inferred types.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.