Agent Skill
2/7/2026

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, or any TypeScript performance investigation.

M
mcart13
0GitHub Stars
1Views
npx skills add mcart13/skills

SKILL.md

Nametypescript-performance-best-practices
DescriptionTypeScript 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, or any TypeScript performance investigation.

name: typescript-performance-best-practices description: 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, or any TypeScript performance investigation.

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 4x faster than intersections
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; 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
  }
}

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

Skills Info
Original Name:typescript-performance-best-practicesAuthor:mcart13