From 83af1abeb53f1ab1001ea53f8f2ca3e8c3157925 Mon Sep 17 00:00:00 2001 From: Daria Pardue Date: Mon, 12 Dec 2022 15:08:39 -0500 Subject: [PATCH 1/4] test: better formatting for ts helper tests --- test/types/schema_helpers.test-d.ts | 33 ++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/test/types/schema_helpers.test-d.ts b/test/types/schema_helpers.test-d.ts index c3c2994d5f4..9d1a41819e9 100644 --- a/test/types/schema_helpers.test-d.ts +++ b/test/types/schema_helpers.test-d.ts @@ -10,7 +10,10 @@ import type { WithoutId } from '../../src/mongo_types'; -// InferIdType +/** ---------------------------------------------------------------------- + * InferIdType + */ + expectType>(new ObjectId()); expectType>(1 + 1); expectType>(new ObjectId()); @@ -22,13 +25,20 @@ expectError }>>({}); expectAssignable>(new ObjectId()); expectAssignable>(1 + 1); -// WithId +/** ---------------------------------------------------------------------- + * WithId + */ + expectAssignable>({ _id: new ObjectId() }); expectAssignable>({ _id: new ObjectId(), a: 3 }); expectAssignable>({ _id: new ObjectId() }); expectAssignable>({ _id: 5 }); expectNotType>({ _id: 3 }); +/** ---------------------------------------------------------------------- + * OptionalId + */ + // Changing _id to a type other than ObjectId makes it required: expectNotType>({ a: 3 }); expectNotType>({ a: 3 }); @@ -44,20 +54,33 @@ class MyId {} expectNotType>({ a: 3 }); expectNotType>({ _id: new ObjectId(), a: 3 }); +/** ---------------------------------------------------------------------- + * OptionalUnlessRequiredId + */ + declare function functionReturningOptionalId(): OptionalId<{ _id?: ObjectId | undefined; a: number; }>; -// OptionalUnlessRequiredId expectType>({ a: 3, _id: new ObjectId() }); expectType>(functionReturningOptionalId()); -// WithoutId removes _id whether defined in the schema or not +/** ---------------------------------------------------------------------- + * WithoutId + * + * WithoutId removes _id whether defined in the schema or not + */ + expectType>({ a: 2 }); expectNotType>({ _id: 3, a: 2 }); expectNotType>({ _id: 3, a: 2 }); -// EnhancedOmit fixes a problem with Typescript's built in Omit that breaks discriminated unions +/** ---------------------------------------------------------------------- + * EnhancedOmit + * + * EnhancedOmit fixes a problem with Typescript's built in Omit that breaks discriminated unions + */ + // NODE-3287 // expectNotAssignable>({ // a: 'one' as const From fee9065f8bcc7e671b457dc066ab9e339c8bf8eb Mon Sep 17 00:00:00 2001 From: Daria Pardue Date: Mon, 12 Dec 2022 15:52:49 -0500 Subject: [PATCH 2/4] test: add ts tests for OptionalId wrapping schema with _id --- test/types/schema_helpers.test-d.ts | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/types/schema_helpers.test-d.ts b/test/types/schema_helpers.test-d.ts index 9d1a41819e9..3164b8ab360 100644 --- a/test/types/schema_helpers.test-d.ts +++ b/test/types/schema_helpers.test-d.ts @@ -1,6 +1,7 @@ import { Document, ObjectId } from 'bson'; import { expectAssignable, expectError, expectNotType, expectType } from 'tsd'; +import { Collection, Db } from '../../src'; import type { EnhancedOmit, InferIdType, @@ -54,6 +55,41 @@ class MyId {} expectNotType>({ a: 3 }); expectNotType>({ _id: new ObjectId(), a: 3 }); +// OptionalId assignability when wrapping a schema with _id: ObjectId +type SchemaWithIdType = { + _id: ObjectId; + a: number; +}; +interface SchemaWithIdInterface { + _id: ObjectId; + a: number; +} + +const typeTestCollection = new Collection>({} as Db, 'test'); +const interfaceTestCollection = new Collection>({} as Db, 'test'); +expectAssignable(await typeTestCollection.findOne()); +expectAssignable(await interfaceTestCollection.findOne()); +expectAssignable((await typeTestCollection.find().toArray())[0]); +expectAssignable((await interfaceTestCollection.find().toArray())[0]); +expectAssignable( + (await typeTestCollection.findOneAndDelete({ a: 1 })).value +); +expectAssignable( + (await interfaceTestCollection.findOneAndDelete({ a: 1 })).value +); +expectAssignable( + (await typeTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value +); +expectAssignable( + (await interfaceTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value +); +expectAssignable( + (await typeTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value +); +expectAssignable( + (await interfaceTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value +); + /** ---------------------------------------------------------------------- * OptionalUnlessRequiredId */ From 3121fc5d3d37ffc2f771fb7cf4a2ed4a96e6b807 Mon Sep 17 00:00:00 2001 From: Daria Pardue Date: Mon, 12 Dec 2022 16:05:07 -0500 Subject: [PATCH 3/4] test: ts tests for OptionalId wrapping non-objectId schema --- test/types/schema_helpers.test-d.ts | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/types/schema_helpers.test-d.ts b/test/types/schema_helpers.test-d.ts index 3164b8ab360..1880edae3f4 100644 --- a/test/types/schema_helpers.test-d.ts +++ b/test/types/schema_helpers.test-d.ts @@ -90,6 +90,48 @@ expectAssignable( (await interfaceTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value ); +// OptionalId assignability when wrapping a schema with _id: number +type SchemaWithIdNumberType = { + _id: number; + a: number; +}; +interface SchemaWithIdNumberInterface { + _id: number; + a: number; +} +const typeNumberTestCollection = new Collection>( + {} as Db, + 'test' +); +const interfaceNumberTestCollection = new Collection>( + {} as Db, + 'test' +); +expectAssignable(await typeNumberTestCollection.findOne()); +expectAssignable(await interfaceNumberTestCollection.findOne()); +expectAssignable((await typeNumberTestCollection.find().toArray())[0]); +expectAssignable( + (await interfaceNumberTestCollection.find().toArray())[0] +); +expectAssignable( + (await typeNumberTestCollection.findOneAndDelete({ a: 1 })).value +); +expectAssignable( + (await interfaceNumberTestCollection.findOneAndDelete({ a: 1 })).value +); +expectAssignable( + (await typeNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value +); +expectAssignable( + (await interfaceNumberTestCollection.findOneAndReplace({ a: 1 }, { a: 5 })).value +); +expectAssignable( + (await typeNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value +); +expectAssignable( + (await interfaceNumberTestCollection.findOneAndUpdate({ a: 1 }, { a: 5 })).value +); + /** ---------------------------------------------------------------------- * OptionalUnlessRequiredId */ From 46cbd8d7dbd065a677dbd697499ef571682da557 Mon Sep 17 00:00:00 2001 From: Daria Pardue Date: Wed, 14 Dec 2022 16:04:25 -0500 Subject: [PATCH 4/4] test: declare instead of instantiating collection in ts test --- test/types/schema_helpers.test-d.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/test/types/schema_helpers.test-d.ts b/test/types/schema_helpers.test-d.ts index 1880edae3f4..ddca7a4dca9 100644 --- a/test/types/schema_helpers.test-d.ts +++ b/test/types/schema_helpers.test-d.ts @@ -1,7 +1,7 @@ import { Document, ObjectId } from 'bson'; import { expectAssignable, expectError, expectNotType, expectType } from 'tsd'; -import { Collection, Db } from '../../src'; +import type { Collection } from '../../src'; import type { EnhancedOmit, InferIdType, @@ -64,9 +64,9 @@ interface SchemaWithIdInterface { _id: ObjectId; a: number; } +declare const typeTestCollection: Collection>; +declare const interfaceTestCollection: Collection>; -const typeTestCollection = new Collection>({} as Db, 'test'); -const interfaceTestCollection = new Collection>({} as Db, 'test'); expectAssignable(await typeTestCollection.findOne()); expectAssignable(await interfaceTestCollection.findOne()); expectAssignable((await typeTestCollection.find().toArray())[0]); @@ -99,14 +99,9 @@ interface SchemaWithIdNumberInterface { _id: number; a: number; } -const typeNumberTestCollection = new Collection>( - {} as Db, - 'test' -); -const interfaceNumberTestCollection = new Collection>( - {} as Db, - 'test' -); +declare const typeNumberTestCollection: Collection>; +declare const interfaceNumberTestCollection: Collection>; + expectAssignable(await typeNumberTestCollection.findOne()); expectAssignable(await interfaceNumberTestCollection.findOne()); expectAssignable((await typeNumberTestCollection.find().toArray())[0]);