← Back to TypeScript Course | Chapter 13: TypeScript with Node.js | Lesson 2 of 6

@types/node की जानकारी

@types/node Node.js के built-in APIs के लिए TypeScript type definitions देता है। यह TypeScript को fs, path, process, Buffer, और NodeJS types जैसे modules और globals समझने देता है।
Syntax
typescript
// npm install --save-dev @types/node
import * as fs from "fs";

const value: string | undefined = process.env.VARIABLE_NAME;

Node.js Types Install करना

@types/node install करना Node के built-in APIs के लिए type definitions जोड़ता है — fs, path, process, और बाकी — क्योंकि Node का अपना runtime plain JavaScript है जिसके पास अपने कोई types नहीं हैं।

उदाहरण: Installing Node.js Types

typescript
// npm install --save-dev @types/node
console.log("@types/node provides types for fs, path, process, and more");

Built-in Modules उपयोग करना

@types/node install होने पर, fs या path जैसे built-in modules import करना पूरा autocomplete और type-checked function signatures देता है, TypeScript द्वारा हर import को untyped treat करने के बजाय।

उदाहरण: Using Built-in Modules

typescript
// import * as path from "path";
// const joined: string = path.join("a", "b");
console.log("With @types/node, path.join is fully typed and autocompleted");

process को Type करना

process को type करना — इसके .env, .argv, और .exit() — भी @types/node से आता है, आपको एक environment variable name में typo या process.exit के लिए गलत argument count compile time पर पकड़ने देते हुए।

उदाहरण: Typing process

typescript
// process.env.NODE_ENV is typed as string | undefined
// process.exit(0) requires a number argument
console.log("process.env and process.exit are typed via @types/node");

NodeJS Types

@types/node में bundled NodeJS namespace Node-specific concepts के लिए types देता है जैसे NodeJS.Timeout (Node में setTimeout से returned, browser के return type से अलग) और NodeJS.ProcessEnv।

उदाहरण: NodeJS Types

typescript
// let timeout: NodeJS.Timeout = setTimeout(() => {}, 1000);
console.log("NodeJS.Timeout differs from the browser's setTimeout return type");

Node Types Configure करना

कौन से Node types load होते हैं यह configure करना tsconfig.json के compilerOptions में types array के through किया जाता है, जो मायने रखता है जब आप ambient globals को सिर्फ Node के तक limit करना चाहें, उसी project में browser DOM types के साथ conflicts से बचते हुए।

उदाहरण: Configuring Node Types

typescript
// tsconfig.json: { "compilerOptions": { "types": ["node"] } }
console.log("The types array limits ambient globals to just Node's own");
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. @types/node भूल जाना, इसलिए fs import करना या process उपयोग करना Cannot find name process जैसा error देता है।
  2. @types/node को अपने Node version से match न करने वाले version पर install करना, newer APIs miss करते हुए।
  3. इसे devDependencies के बजाय dependencies में जोड़ना, या इससे runtime code शामिल होने की उम्मीद करना।
🔒

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.