-
Type: Bug
-
Resolution: Duplicate
-
Priority: Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: TypeScript
What problem are you facing?
this:
expectType<OptionalId<{ _id?: number; a: number }>>({ a: 3 });
is a failure because it requires _id: ObjectId
It should be _id?: number | ObjectId.
I believe the fix should target the logic in InferIdType adding a condition that checks for "optional" and returns the union mentioned above instead.
Alternatively, we could try to forbid users from defining an optional _id that is not an ObjectId, since otherwise they could inadvertently introduce inconsistencies into their database by omitting the _id which would result in the database generating an ObjectId as the value.