Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | /** * Common Type Definitions * * Shared types to replace common `any` patterns with proper type safety. * Use these types instead of `any` for better type checking and refactoring safety. */ // ============================================================================= // JSON Value Types // ============================================================================= /** * Represents any valid JSON value. * Use this instead of `any` for API responses or JSON data. */ export type JsonValue = | string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }; /** * Represents a JSON object (key-value pairs). */ export type JsonObject = { [key: string]: JsonValue }; /** * Represents a JSON array. */ export type JsonArray = JsonValue[]; // ============================================================================= // API Types // ============================================================================= /** * Generic API response wrapper. */ export interface ApiResponse<T = unknown> { success: boolean; data?: T; error?: string; message?: string; } /** * Paginated API response. */ export interface PaginatedResponse<T> extends ApiResponse<T[]> { pagination: { page: number; pageSize: number; total: number; totalPages: number; }; } /** * API metadata - use instead of `any` for flexible metadata objects. */ export interface ApiMetadata { [key: string]: string | number | boolean | null | undefined; } // ============================================================================= // Event Handler Types // ============================================================================= /** * Generic event callback type. */ export type EventCallback<T = unknown> = (event: T) => void; /** * Async event callback type. */ export type AsyncEventCallback<T = unknown> = (event: T) => Promise<void>; // ============================================================================= // Form Types // ============================================================================= /** * Generic form values - use instead of `any` for form data. */ export type FormValues = Record<string, string | number | boolean | null | undefined>; /** * Form field value type. */ export type FormFieldValue = string | number | boolean | null | undefined | File | FileList; // ============================================================================= // Error Types // ============================================================================= /** * Standard error with message. */ export interface ErrorWithMessage { message: string; code?: string; details?: unknown; } /** * Type guard to check if an unknown value is an Error. */ export function isError(error: unknown): error is Error { return error instanceof Error; } /** * Type guard to check if an unknown value has a message property. */ export function hasMessage(error: unknown): error is ErrorWithMessage { return ( typeof error === 'object' && error !== null && 'message' in error && typeof (error as ErrorWithMessage).message === 'string' ); } /** * Safely extract error message from unknown error type. */ export function getErrorMessage(error: unknown): string { if (isError(error)) return error.message; if (hasMessage(error)) return error.message; if (typeof error === 'string') return error; return 'An unknown error occurred'; } // ============================================================================= // Database/Prisma Types // ============================================================================= /** * Generic where clause for Prisma queries. * Use this instead of `any` for dynamic where conditions. */ export type WhereClause<T> = Partial<Record<keyof T, unknown>>; /** * Generic select clause for Prisma queries. */ export type SelectClause<T> = Partial<Record<keyof T, boolean>>; /** * Generic order by clause for Prisma queries. */ export type OrderByClause<T> = Partial<Record<keyof T, 'asc' | 'desc'>>; // ============================================================================= // Utility Types // ============================================================================= /** * Makes all properties of T optional and nullable. */ export type DeepPartial<T> = { [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P] | null; }; /** * Extracts the resolved type from a Promise. */ export type Awaited<T> = T extends Promise<infer U> ? U : T; /** * Makes specified keys required while keeping others optional. */ export type RequireKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>; /** * Function type with any arguments - use sparingly. */ export type AnyFunction = (...args: unknown[]) => unknown; /** * Async function type with any arguments - use sparingly. */ export type AsyncFunction = (...args: unknown[]) => Promise<unknown>; // ============================================================================= // Record Types // ============================================================================= /** * String-keyed record with unknown values. * Use instead of `any` for dynamic objects. */ export type UnknownRecord = Record<string, unknown>; /** * String-keyed record with string values. */ export type StringRecord = Record<string, string>; /** * Number-keyed record. */ export type NumberKeyedRecord<T> = Record<number, T>; |