Skip to main content
The Diagnostics API provides access to syntax errors, type errors, and code suggestions. This powers error highlighting and the Problems panel in editors.

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

fileName
string
required
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

fileName
string
required
Path to the source file to check.

Return Value

Returns Diagnostic[] which may not have location information for global errors.
The first call to getSemanticDiagnostics may be slow as it initializes the type system. Subsequent calls are faster.

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

Code that can be removed:
  • Unused variables
  • Unused imports
  • Unreachable code
Functions that could be async:
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

Always call getSyntacticDiagnostics before getSemanticDiagnostics. Syntax errors are faster to compute and may make semantic checking unnecessary.
Diagnostic results are cached by the Language Service. Don’t recompute unless the file has changed.
Only check changed files, not the entire project:
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