typescript.unsafe_any
Correctness
Medium
Detects usage of any type that bypasses type safety.
Why It Matters
Section titled “Why It Matters”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
Example
Section titled “Example”// ❌ Beforefunction process(data: any) { return data.foo.bar.baz; // No type checking!}
const response: any = await fetch(url);// ✅ Afterinterface 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();What Unfault Detects
Section titled “What Unfault Detects”- Explicit
anytype annotations - Implicit
any(when noImplicitAny is off) anyin function parameters
Auto-Fix
Section titled “Auto-Fix”Unfault suggests unknown as a starting point, requiring explicit type checks.
Better Alternatives
Section titled “Better Alternatives”// Unknown - safe anyfunction process(data: unknown) { if (typeof data === 'object' && data !== null) { // Now TypeScript knows it's an object }}
// Generics for flexibilityfunction identity<T>(value: T): T { return value;}
// Type guardsfunction isUser(obj: unknown): obj is User { return typeof obj === 'object' && obj !== null && 'id' in obj;}
// Zod for runtime validationimport { z } from 'zod';const UserSchema = z.object({ id: z.number(), name: z.string() });const user = UserSchema.parse(untrustedData);