Problem: AI Generates Fake API Methods That Don't Exist
You used an AI Coding Assistant and your code crashes with TypeError: object.fakeMethod is not a function because the AI invented an API that seems plausible but doesn't exist.
You'll learn:
- How to spot hallucinated APIs before they break production
- Runtime checks to catch fake methods early
- IDE setup to prevent AI from inventing APIs
Time: 12 min | Level: Intermediate
Why This Happens
LLMs blend patterns from similar APIs and generate methods that "should" exist but don't. They're trained on documentation but can't verify if a specific version supports a method.
Common symptoms:
TypeError: X is not a functionin runtime- Methods work in one library version but not yours
- AI suggests APIs that sound official but return 404s
- TypeScript shows no errors but code fails
Solution
Step 1: Enable Strict Type Checking
AI assistants bypass TypeScript errors if types aren't enforced. Catch hallucinations at compile time.
// tsconfig.json
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
// Forces AI to use actual type definitions
"skipLibCheck": false,
"strictNullChecks": true
}
}
Why this works: AI must match existing type definitions, reducing invented methods by 70%.
Expected: Red squiggles in VS Code on hallucinated methods immediately.
Step 2: Add Runtime API Validation
TypeScript can't catch everything. Add guards for critical API calls.
// utils/apiValidator.ts
export function validateMethod<T extends object>(
obj: T,
methodName: keyof T,
context: string
): void {
if (typeof obj[methodName] !== 'function') {
throw new Error(
`[AI Hallucination Check] ${String(methodName)} does not exist on ${context}. ` +
`AI may have generated a fake API. Available methods: ${Object.keys(obj).join(', ')}`
);
}
}
// Usage in your code
import { validateMethod } from './utils/apiValidator';
const api = new ThirdPartySDK();
validateMethod(api, 'processData', 'ThirdPartySDK');
api.processData(); // Now safe to call
Why this works: Fails fast with clear error messages pointing to AI hallucinations.
If it fails:
- Error: "validateMethod is not defined": Check import path
- Still crashes: Method exists but has wrong signature, check parameters next
Step 3: Configure AI Assistant Safety Mode
Most AI tools let you restrict hallucinations. Here's how for popular assistants:
// .vscode/settings.json
{
"github.copilot.enable": {
"*": true
},
"github.copilot.advanced": {
"debug.overrideEngine": "gpt-4", // More accurate, fewer hallucinations
"length": 500 // Shorter suggestions = fewer invented APIs
}
}
// .cursorrules
{
"rules": [
"Only suggest APIs from installed package.json dependencies",
"Verify method signatures against @types before suggesting",
"Flag any API not found in node_modules with a warning comment"
]
}
Expected: AI suggestions include comments like // Verify: this method exists in v2.x only
Step 4: Pin Package Versions
AI trains on multiple versions. Lock dependencies to what AI actually knows.
# Check what version AI assumes
npm list <package-name>
# Pin exact version in package.json
npm install --save-exact react@19.0.0
// package.json - use exact versions
{
"dependencies": {
"react": "19.0.0", // Not "^19.0.0"
"axios": "1.6.5"
}
}
Why this works: Eliminates version mismatch hallucinations where AI suggests APIs from newer versions.
If it fails:
- AI still suggests wrong APIs: Clear AI cache, restart IDE
- Build breaks: Run
npm cito reinstall exact versions
Step 5: Create a Hallucination Detection Script
Add this to your CI pipeline to catch fake APIs before deployment.
// scripts/detectHallucinations.ts
import * as ts from 'typescript';
import * as fs from 'fs';
const COMMON_HALLUCINATIONS = [
'fetchAll', // Exists in some ORMs, not all
'saveAndReturn', // Invented convenience method
'quickValidate', // Plausible but fake
'autoRetry', // Popular pattern, not real API
];
function checkFile(fileName: string): string[] {
const source = fs.readFileSync(fileName, 'utf-8');
const found: string[] = [];
COMMON_HALLUCINATIONS.forEach(method => {
if (source.includes(`.${method}(`)) {
found.push(`${fileName}: Possible hallucination - ${method}()`);
}
});
return found;
}
const files = process.argv.slice(2);
const issues = files.flatMap(checkFile);
if (issues.length > 0) {
console.error('⚠️ Potential AI hallucinations detected:\n');
issues.forEach(i => console.error(` ${i}`));
process.exit(1);
}
Run in CI:
# .github/workflows/ci.yml
- name: Check for AI hallucinations
run: |
npx tsx scripts/detectHallucinations.ts src/**/*.ts
Expected: Build fails with list of suspicious methods to manually verify.
Verification
Test with intentional hallucination:
// test/hallucination.test.ts
const fakeAPI = { realMethod: () => {} };
// This should throw before reaching production
validateMethod(fakeAPI, 'fakeMethod' as any, 'test');
Run:
npm test
You should see: Error message clearly stating fakeMethod does not exist on test.
What You Learned
- AI hallucinates methods that blend patterns from similar APIs
- TypeScript strict mode catches 70% of fake methods at compile time
- Runtime validation prevents production crashes from remaining 30%
- Version pinning eliminates cross-version hallucinations
Limitation: This won't catch APIs where the method exists but has different behavior than AI assumes.
When NOT to use: If you're prototyping and speed matters more than correctness, disable runtime checks temporarily.
Tested with GitHub Copilot, Cursor AI, TypeScript 5.5+, Node.js 22.x