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

checkJs का Option

checkJs option TypeScript से included JavaScript files को type-check करने के लिए कहता है। यह files को TypeScript में convert करने से पहले errors expose कर सकता है।
Syntax
typescript
{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": true
  }
}

Core Concept

checkJs compiler option TypeScript की type checking को plain .js files तक extend करता है (सिर्फ .ts तक नहीं), explicit type annotations के बजाय JSDoc comments और usage patterns से types infer करते हुए।

उदाहरण: Core Concept

typescript
/**
 * @param {number} x
 * @returns {number}
 */
function double(x) {
  return x * 2;
}
console.log(double(5));

Basic Setup

इसे tsconfig.json में "allowJs": true के साथ "checkJs": true से enable करें, या एक single file पर top पर // @ts-check comment से बिना global config को छुए।

उदाहरण: Basic Setup

typescript
// @ts-check
/**
 * @param {string} name
 */
function greet(name) {
  console.log("Hello, " + name);
}
greet("Ravi");

Typed Example

एक typed example: /** @param {number} x */ function double(x) { return x * 2; } वाली एक .js file को call sites पर पूरी parameter-type checking मिलती है, भले ही file का extension .ts न हो।

उदाहरण: Typed Example

typescript
// @ts-check
/**
 * @param {number} x
 * @returns {number}
 */
function double(x) {
  return x * 2;
}
console.log(double(5)); // call sites get full parameter-type checking

Project Usage

एक असली project में, checkJs legacy .js files में bugs catch करने के लिए useful है जो अभी एक पूरे rewrite के लायक नहीं, lightweight JSDoc annotations उपयोग करते हुए एक पूरी TypeScript conversion के commit करने के बजाय।

उदाहरण: Project Usage

typescript
// @ts-check
/**
 * @typedef {{ name: string, age: number }} User
 */
/** @param {User} user */
function describe(user) {
  return `${user.name} is ${user.age}`;
}
console.log(describe({ name: "Ravi", age: 25 }));

Best Practices

Specific noisy legacy files को individually opt out करने के लिए उनके top पर // @ts-nocheck उपयोग करें, सिर्फ इसलिए project-wide checkJs disable करने के बजाय कि handful of files बहुत ज़्यादा initial errors generate करती हैं।

उदाहरण: Best Practices

typescript
// @ts-nocheck
// Opt a specific noisy legacy file out of checking, instead of disabling
// checkJs project-wide.
console.log("Use @ts-nocheck per-file rather than a global opt-out");
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. checkJs को allowJs के बिना enable करना, इसलिए JavaScript files include नहीं होती।
  2. एक बड़े codebase पर checkJs से errors से flooded होना, // @ts-check per file उपयोग करने के बजाय।
  3. यह उम्मीद करना कि @param {number} x comments runtime पर enforced होंगे।
🔒

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.