"strict": true
in tsconfig.jsonany
type - use unknown
for truly unknown types// Good - Discriminated union with type guard interface RequestPending { status: 'pending'; }
interface RequestSuccess { status: 'success'; data: UserState; }
interface RequestError { status: 'error'; error: Error; }
type RequestState = RequestPending | RequestSuccess | RequestError;
function isSuccess(state: RequestState): state is RequestSuccess { return state.status === 'success'; }
// Good - Generic with constraints function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; } </example>
<example type="invalid"> // Bad - Using any type function processData(data: any) { return data.someProperty; }// Bad - Not handling null case function getUserName(user: { name: string | null }): string { return user.name.toUpperCase(); // Might crash }
// Bad - Using type assertion without checking function processResponse(response: unknown) { const data = response as { id: number }; return data.id; }
// Bad - Mutable array without type safety const items = []; items.push(123); items.push('string'); // Mixed types array </example>