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

allowJs का Option

allowJs compiler option JavaScript files को एक TypeScript project में include होने की permission देता है। यह JavaScript से TypeScript में migration के दौरान useful है।
Syntax
typescript
{
  "compilerOptions": {
    "allowJs": true
  }
}

Core Concept

allowJs compiler option tsc को आपके .ts files के साथ उसी compilation में plain .js files include करने देता है, जो वह option है जो incremental JavaScript-to-TypeScript migration को पहली जगह possible बनाता है।

उदाहरण: Core Concept

typescript
// tsconfig.json: { "compilerOptions": { "allowJs": true } }
console.log("allowJs lets tsc include plain .js files in the same compilation");

Basic Setup

इसे tsconfig.json में "allowJs": true से enable करें; default रूप से यह सिर्फ .js files को import और compile होने देता है, उनके contents को type-check किए बिना।

उदाहरण: 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

एक typed example: allowJs on होने पर, एक .ts file import { helper } from './utils.js' कर सकती है और TypeScript helper के लिए इसके usage से types infer करेगा, भले ही utils.js में खुद कोई type annotations न हों।

उदाहरण: 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

एक असली project में, allowJs JS-to-TS migration की शुरुआत में सबसे पहला flag on किया जाता है, codebase को transition के दौरान एक mixed .js/.ts project के रूप में compile होने देते हुए, एक atomic rewrite की ज़रूरत के बजाय।

उदाहरण: 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

allowJs को checkJs के साथ सिर्फ तब pair करें जब आप .js files के अंदर ही type errors catch करना शुरू करने के लिए तैयार हों — checkJs को off रखना early stages में migration noise कम रखता है।

उदाहरण: 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. #}
आम गलतियां
  1. allowJs on करना और यह उम्मीद करना कि .js files type-checked होंगी, जब वे सिर्फ compile होती हैं जब तक checkJs on न हो।
  2. allowJs को बिना outDir के set करना, इसलिए output .js original source files को overwrite करता है।
  3. कोई types न रखने वाली .js file import करना और यह realize न करना कि इसके सभी exports 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.