Overview
TypeScript provides three types of diagnostics:Syntactic
Fast syntax-only errors
Semantic
Type system errors
Suggestions
Code improvement hints
getSyntacticDiagnostics
Returns syntax errors in a file without type checking.Signature
Path to the source file to check.
Return Value
Characteristics
Fast
No type checking required
Local
Only analyzes single file
Always Available
Works even with type errors
Subset
Some errors need semantics
Example: Syntactic Diagnostics
Syntax Errors
getSemanticDiagnostics
Returns type system errors and warnings for a file.Signature
Path to the source file to check.
Return Value
ReturnsDiagnostic[] which may not have location information for global errors.
Characteristics
Slower
Requires type checking
Global
Analyzes across files
Complete
All type errors
Incremental
Caches results
Example: Semantic Diagnostics
Type Errors
getSuggestionDiagnostics
Returns suggestion diagnostics that proactively recommend improvements.Signature
Suggestion Types
Unnecessary Code
Unnecessary Code
Code that can be removed:
- Unused variables
- Unused imports
- Unreachable code
Async/Await Opportunities
Async/Await Opportunities
Functions that could be async:
Deprecated APIs
Deprecated APIs
Usage of deprecated APIs:
Example: Suggestions
Suggestion Diagnostics
getCompilerOptionsDiagnostics
Returns diagnostics related to compiler options and configuration.Signature
Example: Configuration Errors
Config Diagnostics
Diagnostic Categories
Filtering by Category
Filter Diagnostics
Formatting Diagnostics
Format for Display
Diagnostic Message Chains
Some diagnostics have nested messages:Flattening Message Chains
Flatten Messages
Real-World Integration
Complete Diagnostic Provider
Performance Tips
Check Syntax First
Check Syntax First
Always call
getSyntacticDiagnostics before getSemanticDiagnostics. Syntax errors are faster to compute and may make semantic checking unnecessary.Cache Results
Cache Results
Diagnostic results are cached by the Language Service. Don’t recompute unless the file has changed.
Incremental Checking
Incremental Checking
Only check changed files, not the entire project:
Debounce in Editors
Debounce in Editors
Don’t check on every keystroke. Debounce diagnostic requests:
See Also
Language Service API
Main Language Service reference
Completions
Code completion API
Program API
Program and compilation
Type Checker
Type system internals