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

TypeScript के साथ SWC

SWC Rust में लिखा एक fast compiler है जो TypeScript और modern JavaScript transform कर सकता है। यह full TypeScript type checking के बजाय fast compilation पर focus करता है।
Syntax
typescript
// .swcrc
{
  "jsc": {
    "parser": { "syntax": "typescript" }
  }
}

// npx swc src -d dist

Core Concept

SWC (Speedy Web Compiler) एक Rust-based compiler है जो TypeScript को standard tsc compiler से significantly faster JavaScript में transpile करता है, Next.js और Jest जैसे tools द्वारा एक drop-in transform step के रूप में उपयोग होता है।

उदाहरण: Core Concept

typescript
// SWC is a Rust-based compiler, much faster than tsc for transpilation
console.log("SWC transpiles TypeScript to JavaScript at high speed");

Basic Setup

एक basic setup @swc/core और @swc/cli install करता है, फिर .swcrc को jsc.parser के साथ typescript पर set करके configure करता है, swc को command line से या एक build tool integration के through .ts files compile करने देते हुए।

उदाहरण: Basic Setup

typescript
// npm install --save-dev @swc/core @swc/cli
// .swcrc: { "jsc": { "parser": { "syntax": "typescript" } } }
console.log("swc CLI compiles .ts files once .swcrc is configured");

Typed Example

esbuild की तरह, SWC TypeScript types को validate किए बिना strip करता है, इसलिए सिर्फ SWC से compiled एक "typed example" फिर भी चलेगा चाहे इसमें असली type errors हों — type checking separately tsc से होना चाहिए।

उदाहरण: Typed Example

typescript
// SWC strips types without validating them, same tradeoff as esbuild
console.log("Code with real type errors can still compile and run via SWC");

Project Usage

एक असली project में, SWC commonly Jest जैसे tools में transform layer के रूप में Babel को replace करता है (@swc/jest के through) specifically बड़े codebases पर test-suite compile time घटाने के लिए।

उदाहरण: Project Usage

typescript
// jest.config.js: transform: { "^.+\\.tsx?$": "@swc/jest" }
console.log("SWC commonly replaces Babel as Jest's transform layer");

Best Practices

अपने CI pipeline में एक dedicated type-checking step के रूप में tsc --noEmit चलाएं जब भी SWC primary compiler हो, क्योंकि SWC की speed exactly type-checking phase को पूरी तरह skip करने से आती है।

उदाहरण: Best Practices

typescript
// CI step: tsc --noEmit
console.log("Run tsc --noEmit separately since SWC skips type checking");
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. यह मान लेना कि SWC type errors catch करता है, जब यह सिर्फ types remove करता है और आपको फिर भी tsc --noEmit चाहिए।
  2. .swcrc में jsc.parser.syntax: "typescript" भूल जाना, इसलिए .ts files parse होने में fail होती हैं।
  3. SWC parser options में enable किए बिना decorators उपयोग करना।
🔒

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.