Skip to main content

Overview

The TypeScript Language Service provides rich IDE features like autocompletion, navigation, refactoring, and diagnostics. It’s built on top of the compiler but designed for interactive, incremental operation.
The language service powers editors like VS Code, Visual Studio, Sublime Text, and many others through the Language Server Protocol.

Architecture

Location: src/services/ (168+ TypeScript files) The language service layer sits between editors and the TypeScript compiler:

Core Components

Language Service

Main service interface providing IDE features

Document Registry

Caches parsed files and manages versions

TSServer

Server process handling editor requests

Protocol

JSON-based communication protocol

Language Service Interface

Location: src/services/services.ts The main service interface exposes IDE features:

Key Features Implementation

Completions

Location: src/services/completions.ts The completion engine provides intelligent suggestions:
1

Context Analysis

Determine completion location (member access, import, type position)
2

Symbol Collection

Gather available symbols from scope, type checker, and imports
3

Filtering

Filter symbols by prefix and context appropriateness
4

Ranking

Sort by relevance using various heuristics
5

Details Generation

Provide type info, documentation, and auto-import suggestions
Completions are position-based and incremental - they don’t require full type checking of the entire program.

Go to Definition

Location: src/services/goToDefinition.ts Navigates from usage to declaration:
Definition Resolution
For local variables, functions, and classes - finds the declaration in the same file
Follows imports to the original declaration in another file or module
For library types, navigates to .d.ts declaration files
Handles function overloads, merged declarations, and augmentations

Find All References

Location: src/services/findAllReferences.ts Finds all usages of a symbol across the project:
1

Symbol Identification

Get the symbol at the current position
2

Related Symbols

Find related symbols (implementations, overrides, aliases)
3

File Search

Search source files for references
4

Context Classification

Classify references (read, write, declaration)
5

Results Grouping

Group results by file and context

Rename

Location: src/services/rename.ts Renames symbols across all files while preserving semantics:
Rename is complex - it must handle scope conflicts, imports/exports, and string literal references.
Rename Process

Code Fixes

Location: src/services/codefixes/ Provides quick fixes for diagnostics:

Import Fixes

Add missing imports automatically

Type Fixes

Add missing type annotations

Declaration Fixes

Declare missing variables or properties

Spelling Fixes

Suggest corrections for typos
Each fix is implemented as a separate module:

Refactorings

Location: src/services/refactors/ Provides structural code transformations:
Refactoring modules:

Document Registry

Location: src/services/documentRegistry.ts Manages source file caching and versioning:
The registry enables incremental compilation by caching and reusing source files across program instances.

TSServer Protocol

Location: src/server/protocol.ts Defines the JSON-based communication protocol:

Request/Response Model

Command Types

TSServer supports 100+ commands:
  • open - Open file for editing
  • close - Close file
  • change - Update file content
  • reload - Reload file from disk

Performance Optimizations

The language service reuses Program instances across edits when possible
Only modified files are reparsed; unchanged subtrees are reused
Type checking happens on-demand for visible files only
Long operations can be cancelled when new edits arrive
Diagnostics are computed in background (async geterr command)

Cancellation Tokens

Cancellable Operations

Additional Features

Formatting

Location: src/services/formatting/ Provides code formatting based on rules:

Format Document

Format entire file

Format Selection

Format selected range

Format On Type

Format as user types

Format On Paste

Format pasted content

Organize Imports

Location: src/services/organizeImports.ts Sorts and removes unused imports:

Call Hierarchy

Location: src/services/callHierarchy.ts Provides call graph navigation:

Inlay Hints

Location: src/services/inlayHints.ts Provides inline hints in editor:

Editor Integration

Editors integrate via different approaches:

Testing

Location: src/harness/ The language service has extensive tests:

Debugging Tips

1

Enable Logging

Set TSS_LOG environment variable to log TSServer requests/responses
2

Inspect Protocol

Use --logVerbosity verbose to see detailed protocol messages
3

Profile Performance

Use --enableTracing to generate performance traces
4

Debug TSServer

Attach debugger to TSServer process for breakpoints

Contributing to Language Service

Language service changes must be fast and not block the editor UI.

Best Practices

Accept and check CancellationToken in long operations
Use position-based APIs and early exits
Cache expensive computations with proper invalidation
Verify features work correctly with incremental updates

Next Steps

Architecture Overview

Review the overall TypeScript architecture

Compiler Internals

Deep dive into compiler implementation

Reference Files

Key language service files in src/services/:
  • services.ts - Main LanguageService interface
  • completions.ts - Completion engine
  • goToDefinition.ts - Definition navigation
  • findAllReferences.ts - Reference finding
  • rename.ts - Symbol renaming
  • documentRegistry.ts - File caching
  • codefixes/ - Quick fix providers
  • refactors/ - Refactoring providers
  • formatting/ - Code formatting
The language service is designed to be responsive and incremental - study how it avoids blocking operations.