-
Type: Bug
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: None
-
Component/s: Native
What problem are you facing?
Projection<T>'s definition seems to be too narrow; specifically, <field>: <expression> type support is missing even though a projection containing a string literal expression runs perfectly fine (all my unit tests still pass) and, if I'm reading it correctly, is described in the documentation as a supported feature.
Here is the offending code from my current project:
const publicMemeProjection = { _id: false, meme_id: { $toString: '$_id' }, owner: { $toString: '$owner' }, receiver: { $toString: '$receiver' }, createdAt: true, expiredAt: true, description: true, likes: '$totalLikes', // <== cause of TS2345 error private: true, replyTo: { $toString: '$replyTo' }, imageUrl: true }; ... const memes = await (await getDb()) .collection<InternalMeme>('memes') .find({ _id: { $in: meme_ids } }) .sort({ _id: -1 }) .limit(getEnv().RESULTS_PER_PAGE) .project<PublicMeme>(publicMemeProjection) // <== location of TS2345 error .toArray();
Which causes the following error:
error TS2345: Argument of type '{ _id: boolean; meme_id: { $toString: string; }; owner: { $toString: string; }; receiver: { $toString: string; }; createdAt: boolean; expiredAt: boolean; description: boolean; likes: string; private: boolean; replyTo: { ...; }; imageUrl: boolean; }' is not assignable to parameter of type 'Projection<PublicMeme>'. Type '{ _id: boolean; meme_id: { $toString: string; }; owner: { $toString: string; }; receiver: { $toString: string; }; createdAt: boolean; expiredAt: boolean; description: boolean; likes: string; private: boolean; replyTo: { ...; }; imageUrl: boolean; }' is not assignable to type '{ createdAt?: boolean | 0 | 1 | ProjectionOperators | undefined; expiredAt?: boolean | 0 | 1 | ProjectionOperators | undefined; description?: boolean | ... 3 more ... | undefined; ... 6 more ...; likes?: boolean | ... 3 more ... | undefined; }'. Types of property 'likes' are incompatible. Type 'string' is not assignable to type 'boolean | 0 | 1 | ProjectionOperators | undefined'. ==> .project<PublicMeme>(publicMemeProjection)
Related: @types/mongodb doesn't type the argument passed to project().
What driver and relevant dependency versions are you using?
- Node.js native driver mongodb@4.0.0
- Using latest MongoDb 4.x
- Previously used @types/mongodb
Steps to reproduce?
See above.
- duplicates
-
NODE-3455 ProjectionOperators type is too narrow in Node.js driver
- Closed
-
NODE-3540 Type error in project
- Closed
- is related to
-
NODE-3468 Type definitions are buggy
- Closed
-
NODE-3455 ProjectionOperators type is too narrow in Node.js driver
- Closed
- related to
-
NODE-4654 Automatic type narrowing for projections [Community Request]
- Backlog