← Back to TypeScript Course | Chapter 25: Build Tools | Lesson 2 of 6

TypeScript के साथ Vite

Vite TypeScript projects के लिए एक fast development server और production build pipeline प्रदान करता है। यह modern ES modules और frontend frameworks के साथ अच्छी तरह काम करता है।
Syntax
typescript
npm create vite@latest project-name -- --template vanilla-ts

Core Concept

Vite एक dev server और bundler है जो development के दौरान आपकी source files को native ES modules पर serve करता है (एक पूरी bundle step को पूरी तरह skip करते हुए) और production build के लिए Rollup पर switch करता है, TypeScript support out of the box built in के साथ।

उदाहरण: Core Concept

typescript
// Vite serves native ES modules in dev, bundles with Rollup for production
console.log("Vite supports TypeScript out of the box, no config needed");

Basic Setup

npm create vite@latest -- --template vanilla-ts (या react-ts, vue-ts, आदि) से scaffolding एक working tsconfig.json और dev server generate करता है बिना typed code लिखना शुरू करने के लिए किसी extra configuration की ज़रूरत के।

उदाहरण: Basic Setup

typescript
// npm create vite@latest my-app -- --template vanilla-ts
console.log("Scaffolds a working tsconfig.json and dev server instantly");

Typed Example

Vite speed के लिए esbuild के through file-by-file TypeScript transpile करता है लेकिन dev server के दौरान type-check नहीं करता — vue-tsc/tsc --noEmit को vite build के साथ चलाना वह है जो असल में type errors catch करता है।

उदाहरण: Typed Example

typescript
// Vite transpiles via esbuild but doesn't type-check during dev
// vue-tsc / tsc --noEmit catches real type errors
console.log("Vite's dev server is fast because it skips type checking");

Project Usage

एक असली project में, Vite का instant hot-module-replacement मतलब है कि एक typed component edit करना browser में लगभग तुरंत reflect होता है, क्योंकि पूरे bundle के बजाय सिर्फ changed module re-transpile होता है।

उदाहरण: Project Usage

typescript
// Editing a .ts file triggers instant HMR for just that module
console.log("Vite's HMR only re-transpiles the changed module");

Best Practices

Type-checking को एक separate step के रूप में रखें (CI या एक pre-commit hook में चलाई एक tsc --noEmit script) क्योंकि Vite का अपना dev/build pipeline speed के लिए intentionally इसे skip करता है और अपने आप type errors catch नहीं करेगा।

उदाहरण: Best Practices

typescript
// package.json: "build": "tsc --noEmit && vite build"
console.log("Run tsc --noEmit as a separate type-checking step");
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. यह मान लेना कि vite dev के दौरान type-check करता है, जब यह सिर्फ esbuild से types strip करता है, इसलिए errors सिर्फ तब दिखते हैं जब आप tsc चलाते हैं।
  2. const enum या namespace features उपयोग करना जिन्हें full type information चाहिए, जिन्हें esbuild अच्छी तरह support नहीं करता।
  3. isolatedModules और type-only import type भूल जाना, re-exported types को fail करने का कारण बनते हुए।
🔒

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.