-
Notifications
You must be signed in to change notification settings - Fork 19
Description
FIle:
db.config.ts
Description:
The connectDB
function currently accepts MONGODB_URI
with the type any
:
export const connectDB = (MONGODB_URI: any) => {}
Using any
defeats the purpose of TypeScript’s static type checking and may lead to unexpected runtime behavior. Since MONGODB_URI
is expected to be a connection string, the appropriate type should be string
.
Recommended Fix
Change the function signature to:
export const connectDB = (MONGODB_URI: string) => { ... }
This improves type safety and aligns with TypeScript best practices.
Benefit
- Enforces type correctness
- Prevents runtime errors
- Improves code readability and maintainability
Additional Suggestion
To prevent errors from an empty or undefined connection string, introduce a guard clause:
if (!MONGODB_URI || MONGODB_URI.trim() === "") { throw new Error("MONGODB_URI is required and must be a non-empty string."); }