This enhanced TypeScript compiler provides AI-friendly diagnostic output designed for automated code analysis, intelligent development tools, and AI-driven code generation systems.
--aiDiagnostics: Human-readable AI-enhanced format with visual formatting--structuredOutput: Comprehensive JSON format for programmatic consumption--machineReadable: Simplified text format for parsing by automation tools
- Confidence scoring for fix suggestions (0-1 scale)
- Pattern recognition based on common TypeScript issues
- Context-aware suggestions that understand code environment
- Complexity assessment for prioritizing fixes
- Estimated fix times for project planning
- Semantic categorization of errors (syntax, type-checking, semantic, etc.)
- Fix patterns with reusable templates
- Dependencies tracking for suggestions that require additional packages
- Success rate estimation based on historical analysis
node tsc.js --aiDiagnostics --noEmit myfile.tsOutput includes:
- Visual error summaries with confidence metrics
- Contextual code snippets
- AI-generated fix suggestions with examples
- Common pattern recommendations
node tsc.js --structuredOutput --noEmit myfile.tsPerfect for:
- AI tool integration
- Automated code analysis
- CI/CD pipeline integration
- Code quality dashboards
node tsc.js --machineReadable --noEmit myfile.tsIdeal for:
- Log parsing systems
- Automated reporting
- Integration with existing tools
- Batch processing
import { convertToAIDiagnostics } from './aiDiagnostics';
const diagnostics = program.getSemanticDiagnostics();
const aiDiagnostics = convertToAIDiagnostics(diagnostics, {
aiContext: true,
patternSuggestions: true,
suggestionConfidence: 0.7
});
// Use AI diagnostics to generate fixes
for (const diag of aiDiagnostics) {
if (diag.suggestions && diag.suggestions[0].confidence > 0.8) {
applySuggestion(diag.suggestions[0]);
}
}import { generateAIErrorSummary } from './aiDiagnostics';
const summary = generateAIErrorSummary(diagnostics, {
aiErrorSummary: true,
semanticHints: true
});
// Prioritize review based on AI analysis
const priorities = summary.mostCommonIssues
.filter(issue => issue.difficulty === 'easy')
.sort((a, b) => b.confidence - a.confidence);// Extract patterns for machine learning
const patterns = aiDiagnostics.map(diag => ({
errorCode: diag.code,
context: diag.context,
successfulFix: diag.suggestions?.find(s => s.confidence > 0.9),
codePattern: diag.location?.snippet
}));
// Feed to ML system for pattern recognition
trainFixPatternModel(patterns);All custom types, interfaces, and type aliases must be defined in a central directory (e.g., types/ or src/types/).
- Do not define types inline in feature or implementation files unless they are truly private to that file.
- All shared types must be imported from the central registry.
- This prevents type sprawl, ambiguity, and duplication—especially important for AI-driven codebases.
src/
types/
user.ts
product.ts
features/
userFeature.ts
productFeature.ts
// src/types/user.ts
export interface User { id: string; name: string; }
// src/features/userFeature.ts
import { User } from '../types/user';
const u: User = { id: '1', name: 'Alice' };- Detect types defined outside the central registry
- Warn on duplicate or ambiguous type definitions
- Suggest moving types to the central registry
- Provide 'why' explanations: "Centralizing types improves maintainability, enables better AI codegen, and prevents ambiguity."
- Offer one-click fixes: Move type to registry and update imports
- Never define shared types inline—always use the central registry.
- Review type imports: All type references should come from the registry.
- Automate type hygiene: Use static analysis or AI diagnostics to enforce these rules.
- Document all types: Add JSDoc or comments to every exported type.
- Refactor regularly: Consolidate or rename types as the project evolves.
interface AIDiagnostic {
code: number; // TypeScript error code
severity: "error" | "warning" | "suggestion" | "info";
category: string; // Semantic category (syntax, type-checking, etc.)
message: string; // AI-enhanced error message
originalMessage: string; // Original TypeScript message
why?: string; // Explanation: Why did this happen?
highConfidenceFix?: boolean; // True if a one-click fix is available (confidence > 0.9)
location?: {
file: string;
line: number;
column: number;
length: number;
snippet: string; // Problematic code
context: {
before: string[]; // Context lines before
after: string[]; // Context lines after
};
};
suggestions?: AISuggestion[]; // Fix suggestions with confidence
context?: {
intent?: string; // What developer was trying to do
patterns?: string[]; // Common fix patterns
symbols?: string[]; // Related symbols/types
examples?: string[]; // Working examples
missingDependencies?: string[];
};
rule?: {
name: string;
description: string;
category: string;
documentation?: string; // Link to docs
};
astContext?: {
nodeType: string;
parentType?: string;
expectedTypes?: string[];
actualType?: string;
};
}interface AISuggestion {
confidence: number; // 0-1 reliability score
description: string; // Human-readable fix description
fix: {
range: { start: number; end: number };
newText: string;
type: "add" | "replace" | "delete";
};
reasoning: string; // Why this fix is suggested
why?: string; // Explanation for this specific fix
dependencies?: string[]; // Required packages/tools
example?: string; // Code example
category?: string; // Fix category
complexity?: "trivial" | "easy" | "medium" | "hard";
estimatedSuccessRate?: number; // Historical success rate
}Each AIDiagnostic now includes a why field, providing a concise, human-readable explanation of the root cause of the error. This helps developers and AI systems understand not just what went wrong, but why it happened, enabling better learning and more targeted fixes.
Diagnostics with a highConfidenceFix: true flag indicate that a fix suggestion is available with confidence > 0.9. These are safe for "one-click" application in editors and automation tools. Integrations (e.g., VS Code) should surface a "Fix" button for these cases.
import * as vscode from 'vscode';
import { convertToAIDiagnostics } from './aiDiagnostics';
const diagnostics = await getTypeScriptDiagnostics();
const aiDiagnostics = convertToAIDiagnostics(diagnostics);
for (const diag of aiDiagnostics) {
if (diag.highConfidenceFix && diag.suggestions?.length) {
// Show "Fix" button for one-click fix
vscode.languages.registerCodeActionsProvider('typescript', {
provideCodeActions: () => [
new vscode.CodeAction('Fix: ' + diag.suggestions[0].description, vscode.CodeActionKind.QuickFix)
]
});
}
if (diag.why) {
// Show "Why did this happen?" explanation in hover or panel
vscode.window.showInformationMessage(`Why: ${diag.why}`);
}
}Suppose you have a missing property error:
interface User { name: string; }
const user: User = {}; // Error: Property 'name' is missingAI Diagnostic output:
{
"code": 2322,
"severity": "error",
"category": "type-checking",
"message": "Type '{}' is missing the following properties from type 'User': name",
"originalMessage": "Type '{}' is missing the following properties from type 'User': name",
"why": "You assigned an object to 'User', but did not provide the required 'name' property. TypeScript enforces required properties for type safety.",
"highConfidenceFix": true,
"suggestions": [
{
"confidence": 0.98,
"description": "Add missing property 'name' to object.",
"fix": { "range": { "start": 27, "end": 29 }, "newText": "name: ''", "type": "replace" },
"reasoning": "The 'User' interface requires a 'name' property. Adding it will satisfy the type checker.",
"why": "The fix adds the required property so the object matches the interface."
}
]
}- name: Check for One-Click Fixes
run: |
npx tsc --structuredOutput --noEmit > ai-diagnostics.json
node -e "const d=require('./ai-diagnostics.json'); if(d.some(x=>x.highConfidenceFix)){process.exit(1)}"
# Fails build if any high-confidence fix is present (enforce clean code)- Always provide a 'why' explanation for every diagnostic surfaced to the user or AI system.
- Only set 'highConfidenceFix' to true if the fix is safe and has confidence > 0.9.
- Surface 'why' explanations in UI (hover, panel, or logs) to help users learn and avoid repeat mistakes.
- Automate one-click fixes in editors and CI/CD for high-confidence cases, but require review for lower-confidence suggestions.
- Test your integration: Add tests that check for the presence and correctness of 'why' and 'highConfidenceFix' fields in output.
- > 0.9: Safe for automatic application
- 0.7-0.9: Good for suggestions with user confirmation
- 0.5-0.7: Useful for learning and pattern recognition
- < 0.5: Research or edge cases
// Prioritize by impact and confidence
const prioritized = aiDiagnostics
.filter(d => d.severity === 'error')
.sort((a, b) => {
const aScore = (a.suggestions?.[0]?.confidence || 0) *
(a.category === 'syntax' ? 1.2 : 1.0);
const bScore = (b.suggestions?.[0]?.confidence || 0) *
(b.category === 'syntax' ? 1.2 : 1.0);
return bScore - aScore;
});// Extract fix patterns for ML training
const extractPatterns = (diagnostics: AIDiagnostic[]) => {
return diagnostics.map(diag => ({
input: {
errorCode: diag.code,
context: diag.location?.snippet,
category: diag.category
},
output: {
fixType: diag.suggestions?.[0]?.fix.type,
pattern: diag.suggestions?.[0]?.category,
success: diag.suggestions?.[0]?.confidence
}
}));
};--aiDiagnostics: Enable AI-enhanced output format--structuredOutput: Output comprehensive JSON--machineReadable: Output parseable text format--aiContext: Include additional context for AI processing--suggestionConfidence <number>: Minimum confidence for suggestions (0-1)--aiErrorSummary: Generate comprehensive error analysis--semanticHints: Include semantic and AST information--patternSuggestions: Enable pattern-based fix suggestions
const options: AICompilerOptions = {
aiDiagnostics: true,
structuredOutput: false,
machineReadable: false,
aiContext: true,
suggestionConfidence: 0.7,
aiErrorSummary: true,
semanticHints: true,
patternSuggestions: true
};// In your extension
const diagnostics = await getTypeScriptDiagnostics();
const aiDiagnostics = convertToAIDiagnostics(diagnostics, {
aiContext: true,
patternSuggestions: true
});
// Show AI-enhanced quick fixes
for (const diag of aiDiagnostics) {
if (diag.suggestions) {
vscode.languages.registerCodeActionsProvider('typescript', {
provideCodeActions: () => diag.suggestions.map(createQuickFix)
});
}
}# GitHub Actions example
- name: TypeScript AI Analysis
run: |
npx tsc --structuredOutput --noEmit > analysis.json
node process-ai-diagnostics.js analysis.json// Watch mode with AI diagnostics
const watcher = ts.createWatchProgram({
// ... config
onDiagnostic: (diagnostic) => {
const aiDiag = convertToAIDiagnostics([diagnostic])[0];
if (aiDiag.suggestions?.[0]?.confidence > 0.8) {
console.log(`🤖 AI Suggestion: ${aiDiag.suggestions[0].description}`);
}
}
});- AI processing adds ~10-15% memory overhead
- Structured output increases JSON size by ~2-3x
- Context extraction requires additional AST traversal
- AI diagnostics: +5-10ms per diagnostic
- Pattern matching: +2-5ms per diagnostic
- Structured output: +1-2ms per diagnostic
// Only enable AI features when needed
const lightweightOptions = {
aiDiagnostics: false,
structuredOutput: true, // Fast JSON output only
suggestionConfidence: 0.8 // Filter low-confidence suggestions
};
// For production builds
const productionOptions = {
aiDiagnostics: false,
structuredOutput: false,
machineReadable: true // Minimal overhead
};- Cross-project learning: Share fix patterns across projects
- Custom pattern training: Train on your codebase's specific patterns
- Real-time collaboration: Share AI insights across team members
- Integration APIs: REST/GraphQL endpoints for AI diagnostics
- Performance profiling: AI-driven performance optimization suggestions
- Natural language queries: "Show me all type errors related to React props"
- Fix automation: Automatically apply high-confidence fixes
- Code generation: Generate code based on error patterns
- Refactoring suggestions: AI-driven code improvement recommendations
Contributions welcome! Areas of interest:
- New fix patterns for common errors
- Performance optimizations
- Integration with popular development tools
- Machine learning model improvements
This enhanced TypeScript compiler makes AI-driven development more accessible and effective by providing structured, intelligent diagnostic information that both humans and machines can understand and act upon.