TypeScript के साथ SWC
In this page:
// .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
// 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
// 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
// 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
// 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
// CI step: tsc --noEmit
console.log("Run tsc --noEmit separately since SWC skips type checking");
- यह मान लेना कि SWC type errors catch करता है, जब यह सिर्फ types remove करता है और आपको फिर भी
tsc --noEmitचाहिए। .swcrcमेंjsc.parser.syntax: "typescript"भूल जाना, इसलिए.tsfiles parse होने में fail होती हैं।- SWC parser options में enable किए बिना decorators उपयोग करना।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: