typescript-performance-best-practices

TypeScript performance optimization guidelines for build times, type-checking, declaration emit, and editor responsiveness. This skill should be used when writing, reviewing, or optimizing TypeScript code and tsconfig settings. Triggers on tasks involving slow builds, slow type-checking, CI timeouts, editor lag, tsserver memory issues, monorepo configuration, project references, incremental builds, declaration emit, isolatedDeclarations, --noCheck flag, TypeScript 5.6+ features, TypeScript 7 migration, or any TypeScript performance investigation.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "typescript-performance-best-practices" with this command: npx skills add mcart13/dev-skills/mcart13-dev-skills-typescript-performance-best-practices

TypeScript Performance Best Practices

Comprehensive performance optimization guide for TypeScript codebases. Contains 43 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.

When to Apply

Reference these guidelines when:

  • Writing or refactoring TypeScript code
  • Tuning build times, type-checking performance, declaration emit, or editor responsiveness
  • Investigating performance regressions
  • Reviewing code for performance issues
  • Configuring tsconfig.json for new projects or monorepos

Quick Diagnostic Workflow

When TypeScript feels slow, follow this diagnostic sequence:

# Step 1: Get baseline metrics
tsc --extendedDiagnostics --noEmit

# Key metrics to check:
# - Files: Should match expected source count
# - Check time: Usually the bottleneck (>10s is high)
# - Instantiations: High count indicates complex generics

# Step 2: If file count is high
tsc --listFiles | wc -l
tsc --explainFiles 2>&1 | grep -v "@types" | head -50

# Step 3: If Check time is high
tsc --generateTrace ./trace --noEmit
# Open chrome://tracing and load trace/trace.json

# Step 4: If module resolution is slow
tsc --traceResolution 2>&1 | head -200

Rule Categories by Priority

PriorityCategoryImpactQuick Win
1Build Graph & IncrementalCRITICALEnable incremental: true
2Program Scope & InputsHIGHNarrow include patterns
3Module Resolution & ImportsHIGHUse moduleResolution: "bundler"
4Type Acquisition & Lib ChecksMEDIUM-HIGHEnable skipLibCheck: true
5Type System ComplexityCRITICALAnnotate function return types
6Declaration Emit & Public APIMEDIUM-HIGHEnable isolatedDeclarations
7Editor/tsserver PerformanceMEDIUM-HIGHUse project references
8Diagnostics & ProfilingDIAGNOSTICRun tsc --extendedDiagnostics

Common Scenarios

Scenario: Slow CI Builds (>60s)

Symptoms: CI takes minutes, incremental doesn't help

Diagnostic:

tsc --extendedDiagnostics --noEmit
# Check: Files count, Check time, Memory used

Likely fixes:

  1. Enable incremental: true with cached .tsbuildinfo
  2. Enable skipLibCheck: true (10-50% improvement)
  3. Split into project references (60-70% memory reduction)
  4. Narrow include patterns

Scenario: Slow Editor/IntelliSense

Symptoms: Autocomplete lags, hover takes seconds, high memory

Diagnostic:

# Check tsserver memory
ps aux | grep tsserver | awk '{print $6/1024 " MB"}'

Likely fixes:

  1. Enable disableReferencedProjectLoad: true
  2. Enable disableSolutionSearching: true
  3. Enable disableSourceOfProjectReferenceRedirect: true
  4. Split into project references

Scenario: Type Checking Takes >10s

Symptoms: Check time dominates in diagnostics

Diagnostic:

tsc --generateTrace ./trace --noEmit
# Open chrome://tracing, find slow checkSourceFile operations

Likely fixes:

  1. Add explicit return type annotations to exported functions
  2. Simplify large unions (50+ members)
  3. Replace deep intersections with interface extension
  4. Name complex types with type aliases

Scenario: Unexpected Files in Build

Symptoms: More files than expected, node_modules source compiled

Diagnostic:

tsc --explainFiles 2>&1 | grep -v "@types" | grep "node_modules"

Likely fixes:

  1. Add "exclude": ["node_modules", "dist"]
  2. Narrow include to ["src"]
  3. Remove broad **/* patterns

Quick Reference

1. Build Graph & Incremental (CRITICAL)

Impact: 50-80% faster rebuilds; essential for monorepos

RuleImpactDescription
build-project-referencesCRITICALSplit large repos into referenced projects
build-compositeCRITICALEnable composite for project reference builds
build-incrementalCRITICALReuse prior type-checking work
build-tsbuildinfoHIGHCache incremental state in stable location
build-tsc-buildHIGHUse tsc --build for reference graphs
build-assume-direct-depsMEDIUMSpeed up watch in very large repos

2. Program Scope & Inputs (HIGH)

Impact: Reduces files parsed; every extra file costs time

RuleImpactDescription
scope-tight-includeHIGHRestrict include to source directories
scope-exclude-build-outputHIGHExclude dist/build/node_modules
scope-avoid-broad-globsMEDIUM-HIGHAvoid **/* in large repos
scope-use-files-arrayMEDIUMExplicit file lists for small packages
scope-separate-testsMEDIUMKeep tests in separate tsconfig

3. Module Resolution & Imports (HIGH)

Impact: Reduces resolution overhead; wrong mode = excessive lookups

RuleImpactDescription
resolve-moduleResolution-modernHIGHUse node16/nodenext/bundler
resolve-bundler-modeHIGHUse bundler mode for bundled apps
resolve-keep-paths-tightMEDIUM-HIGHAvoid catch-all paths patterns
resolve-use-packagejson-exportsMEDIUMUse package.json exports/imports
resolve-baseurl-minimizeMEDIUMUse baseUrl only when needed

4. Type Acquisition & Lib Checks (MEDIUM-HIGH)

Impact: 10-50% faster type-checking; skips redundant work

RuleImpactDescription
types-limit-typesMEDIUM-HIGHInclude only needed @types
types-restrict-typeRootsMEDIUMLimit global type search paths
types-skip-lib-checkHIGHSkip .d.ts validation (major win)
types-typeacquisition-controlMEDIUMControl automatic type acquisition
types-disable-filename-based-acquisitionLOWPrevent filename-based type downloads

5. Type System Complexity (CRITICAL)

Impact: Complex types cause O(n^2) checking; simplification = big wins

RuleImpactDescription
type-annotate-exportsCRITICALNamed exported types reduce emit cost
type-annotate-returnsCRITICALReturn annotations reduce inference
type-name-complex-typesHIGHNamed types reduce expansions
type-avoid-deep-intersectionsHIGHInterfaces cached; intersections recomputed
type-avoid-large-unionsHIGHLarge unions cause O(n*m) checking
type-export-named-typesMEDIUM-HIGHNamed exports reduce .d.ts size

6. Declaration Emit & Public API (MEDIUM-HIGH)

Impact: Up to 3x faster with isolatedDeclarations (requires --noCheck or alternative emitters like swc/oxc to benefit); parallel emit

RuleImpactDescription
dts-emit-declaration-onlyMEDIUM-HIGHSkip JS emit when bundler handles it
dts-declarationDirMEDIUMOrganize .d.ts output separately
dts-strip-internalMEDIUMRemove @internal from .d.ts
dts-isolated-declarationsCRITICALEnable parallel .d.ts emit
dts-noCheck-fast-emitHIGHSkip type-check for .d.ts emit

7. Editor/tsserver Performance (MEDIUM-HIGH)

Impact: ~3GB to <1GB memory; faster startup, navigation

RuleImpactDescription
tsserver-disable-referenced-project-loadMEDIUM-HIGHLoad projects on demand
tsserver-disable-solution-searchingMEDIUM-HIGHStop upward config search
tsserver-disable-source-redirectMEDIUM-HIGHUse .d.ts across boundaries
tsserver-use-project-referencesHIGHSplit projects for memory savings
tsserver-solution-configMEDIUM-HIGHRoot tsconfig with files:[]

8. Diagnostics & Profiling (DIAGNOSTIC)

Purpose: Tools for finding performance issues, not optimizations themselves

RulePurposeWhen to Use
diag-extended-diagnosticsDetailed timing breakdownFirst diagnostic to run
diag-diagnosticsQuick timing overviewCI summaries
diag-explain-filesWhy files are includedUnexpected file count
diag-trace-resolutionModule resolution steps"Cannot find module" errors
diag-generate-traceChrome DevTools traceDeep type-checking analysis
diag-list-filesAll files in compilationScope audits

Recommended Base Configuration

For most TypeScript projects:

{
  "compilerOptions": {
    // Performance essentials
    "incremental": true,
    "skipLibCheck": true,
    "moduleResolution": "bundler",

    // For libraries
    "declaration": true,
    "declarationMap": true,
    "isolatedDeclarations": true,

    // Standard strictness
    "strict": true,
    "noUncheckedIndexedAccess": true,

    // Output
    "target": "ES2022",
    "module": "ESNext"
  },
  "include": ["src"],
  "exclude": ["node_modules", "dist"]
}

For monorepos, add to each package:

{
  "compilerOptions": {
    "composite": true,
    "disableReferencedProjectLoad": true,
    "disableSolutionSearching": true,
    "disableSourceOfProjectReferenceRedirect": true
  }
}

TypeScript 5.6+ Performance Features

Recent TypeScript versions (5.6-5.8) introduced significant performance improvements:

--noCheck Flag (TS 5.6+)

Skip type-checking when you only need transpilation or declaration emit:

# Fast declaration emit without type-checking
tsc --declaration --emitDeclarationOnly --noCheck

# Combine with isolatedDeclarations for maximum speed
tsc --declaration --emitDeclarationOnly --noCheck --isolatedDeclarations

When to use: CI pipelines where type-checking runs separately, or when using swc/esbuild for transpilation.

--libReplacement Flag (TS 5.8+)

Control whether lib.d.ts can be replaced by node_modules types:

{
  "compilerOptions": {
    "libReplacement": false
  }
}

Impact: Prevents unexpected type resolution slowdowns from packages that ship their own lib replacements.

Version Compatibility Table

FeatureMinimum TS VersionImpact
isolatedDeclarations5.5Up to 3x
--noCheck5.62-10x emit
--libReplacement5.8Varies
Swiss Table internals5.85-20% faster

TypeScript 7 Preview (Experimental)

TypeScript 7, currently in development, is being rewritten in Go for significantly faster compilation. Early benchmarks suggest:

  • 10x faster type-checking in large codebases
  • 5x faster editor responsiveness
  • Full backward compatibility with TS 5.x configs

Status (as of January 2026): Preview builds available. Not production-ready. Monitor TypeScript 7 roadmap for updates.

How to Use

Read individual rule files for detailed explanations and code examples:

rules/build-project-references.md
rules/type-annotate-returns.md
rules/diag-generate-trace.md

Each rule file contains:

  • Impact rating with quantified description
  • Why it matters with numbered benefits
  • Incorrect/Correct code examples
  • When NOT to apply (important for avoiding over-optimization)
  • Common mistakes to avoid
  • Diagnostic commands for verification
  • References to official documentation

References

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

go-performance-best-practices

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

rust-performance-best-practices

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

fhir-hl7-validator

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

ralph-lisa-loop

No summary provided by upstream source.

Repository SourceNeeds Review
typescript-performance-best-practices | V50.AI