From 9e2495e9e77fb5e4f6eb8e3daa867ca9eaf7ff6e Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Thu, 14 Oct 2021 12:59:04 -0700 Subject: [PATCH 1/3] Fix compile error on TS 4.5 (beta) Typescript 4.5 is better at checking certain complicated types than before. Previously, it missed an error in mongo_types.ts in the use of SetFields and OptionalId. SetFields passes an unconstrained type parameter, TSchema, to OptionalId, which does have a constraint: ```ts export type OptionalId = ... ``` Because the type was so complex, Typescript missed this before 4.5. Looking at the comments for OptionalId, I believe the intent is to add `_id` to *any* type, not just ones that already have an `_id` field. So I moved the constraint into a conditional type instead. --- src/mongo_types.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index cc73d0d63b..c3637a6cd8 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -38,9 +38,11 @@ export type WithId = EnhancedOmit & { _id: InferIdType< * `TSchema['_id'] extends ObjectId` which translated to "Is the _id property ObjectId?" * we instead ask "Does ObjectId look like (have the same shape) as the _id?" */ -export type OptionalId = ObjectId extends TSchema['_id'] // a Schema with ObjectId _id type or "any" or "indexed type" provided - ? EnhancedOmit & { _id?: InferIdType } // a Schema provided but _id type is not ObjectId - : WithId; // TODO(NODE-3285): Improve type readability +export type OptionalId = TSchema extends { _id?: any } + ? (ObjectId extends TSchema['_id'] // a Schema with ObjectId _id type or "any" or "indexed type" provided + ? EnhancedOmit & { _id?: InferIdType } // a Schema provided but _id type is not ObjectId + : WithId) + : WithId; // TODO(NODE-3285): Improve type readability /** TypeScript Omit (Exclude to be specific) does not work for objects with an "any" indexed type, and breaks discriminated unions @public */ export type EnhancedOmit = string extends keyof TRecordOrUnion From d7421006d8e30f5c8e379b2c835642ea8a7f41c6 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Fri, 15 Oct 2021 11:09:41 -0700 Subject: [PATCH 2/3] Make _id optional in missing case --- src/mongo_types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index c3637a6cd8..21ec43539f 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -42,7 +42,7 @@ export type OptionalId = TSchema extends { _id?: any } ? (ObjectId extends TSchema['_id'] // a Schema with ObjectId _id type or "any" or "indexed type" provided ? EnhancedOmit & { _id?: InferIdType } // a Schema provided but _id type is not ObjectId : WithId) - : WithId; // TODO(NODE-3285): Improve type readability + : EnhancedOmit & { _id?: InferIdType }; // TODO(NODE-3285): Improve type readability /** TypeScript Omit (Exclude to be specific) does not work for objects with an "any" indexed type, and breaks discriminated unions @public */ export type EnhancedOmit = string extends keyof TRecordOrUnion From 3ef9e40e4307b23516544386f12896292d5dd22a Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Fri, 15 Oct 2021 14:44:28 -0400 Subject: [PATCH 3/3] fix: lint --- src/mongo_types.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/mongo_types.ts b/src/mongo_types.ts index 21ec43539f..e911aade8c 100644 --- a/src/mongo_types.ts +++ b/src/mongo_types.ts @@ -38,11 +38,11 @@ export type WithId = EnhancedOmit & { _id: InferIdType< * `TSchema['_id'] extends ObjectId` which translated to "Is the _id property ObjectId?" * we instead ask "Does ObjectId look like (have the same shape) as the _id?" */ -export type OptionalId = TSchema extends { _id?: any } - ? (ObjectId extends TSchema['_id'] // a Schema with ObjectId _id type or "any" or "indexed type" provided - ? EnhancedOmit & { _id?: InferIdType } // a Schema provided but _id type is not ObjectId - : WithId) - : EnhancedOmit & { _id?: InferIdType }; // TODO(NODE-3285): Improve type readability +export type OptionalId = TSchema extends { _id?: any } + ? ObjectId extends TSchema['_id'] // a Schema with ObjectId _id type or "any" or "indexed type" provided + ? EnhancedOmit & { _id?: InferIdType } // a Schema provided but _id type is not ObjectId + : WithId + : EnhancedOmit & { _id?: InferIdType }; // TODO(NODE-3285): Improve type readability /** TypeScript Omit (Exclude to be specific) does not work for objects with an "any" indexed type, and breaks discriminated unions @public */ export type EnhancedOmit = string extends keyof TRecordOrUnion