What is the Language Service?
The Language Service is a high-level API that wraps TypeScript’s compiler (tsc) to provide interactive editing features. While the compiler focuses on transforming TypeScript to JavaScript, the Language Service provides:
- Code intelligence: Completions, quick info, signature help
- Diagnostics: Syntax and semantic error reporting
- Navigation: Go to definition, find references, rename
- Refactoring: Extract method, organize imports, code fixes
- Formatting: Code formatting and indentation
Architecture
Core Components
Language Service
Main API providing editor features
Language Service Host
Interface between editor and Language Service
Program
Manages source files and compilation
Type Checker
Performs type analysis and inference
How tsserver Uses the Language Service
The TypeScript server (tsserver) is the primary consumer of the Language Service:
Request Flow
- Editor sends request (e.g., “get completions at position 42”)
- tsserver receives the request via JSON protocol
- Language Service Host provides file contents via snapshots
- Language Service uses the Program and Type Checker
- Results are returned to the editor
Language Service Modes
The Language Service supports three modes viaLanguageServiceMode:
Semantic (Full)
Semantic (Full)
Complete type checking and semantic analysis. This is the default mode that provides all features including type-aware completions, semantic diagnostics, and refactorings.
PartialSemantic
PartialSemantic
Limited semantic analysis for better performance. Useful for large projects where full type checking may be too slow.
Syntactic (Syntax Only)
Syntactic (Syntax Only)
Only syntax-based features, no type checking. Provides syntax highlighting, basic completions, and formatting without the overhead of type analysis.
Document Registry
TheDocumentRegistry caches parsed source files across multiple Language Service instances:
Benefits
- Memory efficiency: Multiple projects share parsed ASTs
- Performance: Avoid re-parsing unchanged files
- Incremental updates: Reuse parts of the syntax tree
Script Snapshots
Script snapshots provide an immutable view of file contents:- Incremental parsing: Detect which parts of a file changed
- Consistency: Immutable view during operation
- Efficiency: Avoid re-reading files multiple times
Performance Considerations
Optimization Strategies
- Reuse Language Service instances: Don’t recreate for every operation
- Use syntax-only mode when type information isn’t needed
- Implement efficient snapshots: Use incremental change detection
- Cache unchanged files: Return same version string for unchanged files
- Leverage Document Registry: Share parsed files across projects
Version String
The version string returned bygetScriptVersion is crucial:
The Language Service uses version strings to determine if a file has changed. Return the same string for unchanged files to enable caching.
Common Use Cases
Editor Integration
Build Tools
Services Version
The Language Service API version is tracked separately from the TypeScript compiler version:The services API is generally stable, but check compatibility when using advanced features.
Next Steps
Language Service API
Detailed API reference with all methods
Completions
Learn about completion providers
Diagnostics
Error and warning diagnostics
Navigation
Go to definition, find references