check_type_compatibility
Verify if a source type can be assigned to a target type in TypeScript. This tool helps developers validate type compatibility for safer code and better type safety.
Instructions
Check if two types are compatible/assignable
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sourceType | Yes | The source type | |
| targetType | Yes | The target type |
Implementation Reference
- src/type-validator.ts:164-199 (handler)Core handler function that implements the type compatibility check by generating test TypeScript code with an assignment from sourceType to targetType and validating it using the TypeScript compiler diagnostics.
checkTypeCompatibility(sourceType: string, targetType: string): ValidationResult { const errors: ValidationError[] = []; const warnings: ValidationWarning[] = []; try { // Create a test assignment to check compatibility const testCode = ` let source: ${sourceType}; let target: ${targetType}; target = source; // This will fail if types are incompatible `; const result = this.validateTypeUsage(testCode); // Filter out assignment-specific errors and focus on type compatibility const compatibilityErrors = result.errors.filter(error => error.message.includes("not assignable") || error.message.includes("incompatible") ); errors.push(...compatibilityErrors); warnings.push(...result.warnings); } catch (error) { errors.push({ message: `Type compatibility check failed: ${error instanceof Error ? error.message : String(error)}`, code: "COMPATIBILITY_ERROR" }); } return { valid: errors.length === 0, errors, warnings }; } - src/mcp-server.ts:360-375 (handler)MCP server wrapper handler that invokes the core type validator's checkTypeCompatibility and formats the response as MCP content.
private async handleCheckTypeCompatibility(sourceType: string, targetType: string) { const result = this.typeValidator.checkTypeCompatibility(sourceType, targetType); return { content: [ { type: "text", text: JSON.stringify({ sourceType, targetType, ...result }, null, 2) } ] }; } - src/mcp-server.ts:162-179 (registration)Tool registration in the listTools response, defining the tool name, description, and input schema.
{ name: "check_type_compatibility", description: "Check if two types are compatible/assignable", inputSchema: { type: "object", properties: { sourceType: { type: "string", description: "The source type" }, targetType: { type: "string", description: "The target type" } }, required: ["sourceType", "targetType"] } }, - src/mcp-server.ts:25-25 (schema)TypeScript type definition for the tool arguments used in validateArgs.
check_type_compatibility: { sourceType: string; targetType: string }; - src/mcp-server.ts:241-247 (registration)Dispatch case in CallToolRequestSchema handler that routes the tool call to the appropriate handler method.
case "check_type_compatibility": { const compatArgs = this.validateArgs<ToolArguments["check_type_compatibility"]>(args); return await this.handleCheckTypeCompatibility( compatArgs.sourceType, compatArgs.targetType ); }