validate_type_usage
Validate TypeScript code for type correctness and check against expected types to ensure type safety in your projects.
Instructions
Validate TypeScript code for type correctness
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The TypeScript code to validate | |
| expectedType | No | Optional expected type to validate against |
Implementation Reference
- src/mcp-server.ts:292-303 (handler)The primary handler function for the 'validate_type_usage' MCP tool. It receives the code and optional expectedType, calls the TypeValidator's validateTypeUsage method, and formats the result as an MCP text response.private async handleValidateTypeUsage(code: string, expectedType?: string) { const result = this.typeValidator.validateTypeUsage(code, expectedType); return { content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; }
- src/mcp-server.ts:97-110 (schema)Input schema definition for the 'validate_type_usage' tool, specifying the required 'code' parameter and optional 'expectedType'.inputSchema: { type: "object", properties: { code: { type: "string", description: "The TypeScript code to validate" }, expectedType: { type: "string", description: "Optional expected type to validate against" } }, required: ["code"] }
- src/mcp-server.ts:217-220 (registration)Registration of the tool handler in the CallToolRequestSchema switch statement, validating arguments using ToolArguments type and dispatching to the handler.case "validate_type_usage": { const validateArgs = this.validateArgs<ToolArguments["validate_type_usage"]>(args); return await this.handleValidateTypeUsage(validateArgs.code, validateArgs.expectedType); }
- src/type-validator.ts:11-79 (helper)Core helper function implementing the type usage validation by creating a temporary TypeScript program, running diagnostics, collecting errors and warnings, and optionally checking against an expected type.validateTypeUsage(code: string, expectedType?: string): ValidationResult { const errors: ValidationError[] = []; const warnings: ValidationWarning[] = []; try { // Create a temporary source file for validation const sourceFile = ts.createSourceFile( "temp.ts", code, ts.ScriptTarget.Latest, true ); // Create a program with just this file const defaultCompilerHost = ts.createCompilerHost(this.compilerOptions); const compilerHost = { ...defaultCompilerHost, getSourceFile: (fileName: string, languageVersion: ts.ScriptTarget) => { if (fileName === "temp.ts") { return sourceFile; } return defaultCompilerHost.getSourceFile(fileName, languageVersion); } }; const program = ts.createProgram(["temp.ts"], this.compilerOptions, compilerHost); // Get semantic diagnostics const semanticDiagnostics = program.getSemanticDiagnostics(sourceFile); const syntacticDiagnostics = program.getSyntacticDiagnostics(sourceFile); // Convert diagnostics to our format for (const diagnostic of [...syntacticDiagnostics, ...semanticDiagnostics]) { const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); const position = diagnostic.start ? sourceFile.getLineAndCharacterOfPosition(diagnostic.start) : undefined; const errorInfo = { message, line: position ? position.line + 1 : undefined, column: position ? position.character + 1 : undefined, code: diagnostic.code?.toString() }; if (diagnostic.category === ts.DiagnosticCategory.Error) { errors.push(errorInfo); } else if (diagnostic.category === ts.DiagnosticCategory.Warning) { warnings.push(errorInfo); } } // If expected type is provided, validate against it if (expectedType && errors.length === 0) { const typeChecker = program.getTypeChecker(); this.validateExpectedType(sourceFile, typeChecker, expectedType, errors, warnings); } } catch (error) { errors.push({ message: `Validation failed: ${error instanceof Error ? error.message : String(error)}`, code: "VALIDATION_ERROR" }); } return { valid: errors.length === 0, errors, warnings }; }