-
Type:
New Feature
-
Resolution: Unresolved
-
Priority:
Trivial - P5
-
None
-
Affects Version/s: None
-
Component/s: TypeScript
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Use Case
As a... typescript developer
I want... the option to enforce strict type checking of CRUD APIs
So that... anyone working with my codebase that may lack context can rely on type checking to catch issues
For example:
import { Filter, MongoClient, WithId } from 'mongodb'; // Define a simple user document type interface User { name: string; email: string; age: number; } async function main() { const client = new MongoClient('mongodb://localhost:27017'); const db = client.db('test'); const collection = db.collection<User>('users'); // ✅ This correctly compiles - 'name' exists in the User schema await collection.findOne({ name: 'John' }); // ✅ This correctly compiles - '_id' is automatically added by WithId<T> await collection.findOne({ _id: new ObjectId('507f1f77bcf86cd799439011') }); // ❌ PROBLEM: This SHOULD be a TypeScript error, but it compiles successfully! // 'id' is NOT a field in the User interface, yet TypeScript allows it await collection.findOne({ id: '507f1f77bcf86cd799439011' }); // ❌ PROBLEM: Same issue - 'userId' doesn't exist but no error await collection.findOne({ userId: 'some-id' }); // ❌ PROBLEM: Completely made up field - still no error await collection.findOne({ nonExistentField: 'value' }); // The reason this happens is because MongoDB's Filter type includes: // [key: string]: any // which allows any string key to be used as a filter field }
- related to
-
NODE-4365 Improve the usability and strictness of the Filter type
-
- Backlog
-