allowJs का Option
In this page:
{
"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
// 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
// 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
// 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
// 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
// tsconfig.json: { "allowJs": true, "checkJs": false } (initially)
console.log("Add checkJs only once ready to catch type errors inside .js files");
allowJson करना और यह उम्मीद करना कि.jsfiles type-checked होंगी, जब वे सिर्फ compile होती हैं जब तकcheckJson न हो।allowJsको बिनाoutDirके set करना, इसलिए output.jsoriginal source files को overwrite करता है।- कोई types न रखने वाली
.jsfile import करना और यह realize न करना कि इसके सभी exports loosely inferred types के रूप में आते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: