Type Checking की Performance
In this page:
Core Concept
TypeScript का type checker बड़े codebases पर noticeably slow हो सकता है, खासकर deeply nested generics, huge union types, या overly broad include globs के साथ जो उन files को खींच लेते हैं जिन्हें असल में check करने की ज़रूरत नहीं।
उदाहरण: Core Concept
// Deeply nested generics and huge unions slow down the type checker.
console.log("Large codebases can hit real type-checking performance limits");
Basic Setup
एक basic diagnostic setup tsc --extendedDiagnostics (या --generateTrace) चलाता है यह देखने के लिए कि कौन सी files और language features सबसे ज़्यादा check time consume कर रही हैं, bottleneck guess करने के बजाय।
उदाहरण: Basic Setup
// tsc --extendedDiagnostics
console.log("extendedDiagnostics shows exactly what's consuming check time");
Typed Example
एक common slowdown का typed example: सैकड़ों string literal members वाला एक union type compiler को हर assignment check पर हर member compare करने के लिए मजबूर करता है, जो एक narrower type या एक enum से measurably slower है।
उदाहरण: Typed Example
type Status = "a" | "b" | "c"; // a 500-member union would be much slower
function check(s: Status) { return s; }
console.log(check("a"));
Project Usage
एक असली project में, एक monolith को project references (हर एक independently cached) में split करना plus "skipLibCheck": true enable करना आम तौर पर overall check time कम करने के लिए दो सबसे highest-impact changes हैं।
उदाहरण: Project Usage
// tsconfig.json: { "compilerOptions": { "skipLibCheck": true } }
console.log("skipLibCheck plus project references are the biggest speed wins");
Best Practices
Hot-path type definitions में overly clever recursive conditional types से बचें — वे अन्यथा reasonably-sized codebases पर exponential type-checking time का एक common, hard-to-spot source हैं।
उदाहरण: Best Practices
// Avoid deeply recursive conditional types in hot-path type definitions.
type Depth1<T> = T extends string ? "string" : "other";
console.log("Recursive conditional types can cause exponential check time");
tsc --extendedDiagnosticsपहले चलाए बिना slow code guess करना।- Huge unions या deeply nested conditional types लिखना जो checker को slow करते हैं।
- एक बड़े project में
skipLibCheckको off छोड़ना, इसलिएnode_modulesमें.d.tsfiles हर बार re-check होती हैं।
- Monorepos एक repository में कई packages रखते हैं, अक्सर एक types package share करते हुए।
- TypeScript project references और incremental compilation बड़े projects को build करना तेज़ बनाते हैं।
- Type checking performance project बढ़ने के साथ attention देने लायक है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: