Skip to content

typescript.unsafe_any

Correctness Medium

Detects usage of any type that bypasses type safety.

any defeats TypeScript’s purpose:

  • No type checking — Errors pass through compile time
  • Runtime crashes — TypeErrors in production
  • Broken refactoring — Can’t track what uses what
  • Contagious — Spreads through the codebase
// ❌ Before
function process(data: any) {
return data.foo.bar.baz; // No type checking!
}
const response: any = await fetch(url);
// ✅ After
interface Data {
foo: { bar: { baz: string } };
}
function process(data: Data) {
return data.foo.bar.baz; // Type checked
}
interface ApiResponse {
users: User[];
}
const response = await fetch(url);
const data: ApiResponse = await response.json();
  • Explicit any type annotations
  • Implicit any (when noImplicitAny is off)
  • any in function parameters

Unfault suggests unknown as a starting point, requiring explicit type checks.

// Unknown - safe any
function process(data: unknown) {
if (typeof data === 'object' && data !== null) {
// Now TypeScript knows it's an object
}
}
// Generics for flexibility
function identity<T>(value: T): T {
return value;
}
// Type guards
function isUser(obj: unknown): obj is User {
return typeof obj === 'object' && obj !== null && 'id' in obj;
}
// Zod for runtime validation
import { z } from 'zod';
const UserSchema = z.object({ id: z.number(), name: z.string() });
const user = UserSchema.parse(untrustedData);