Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@types/mongodb] Redefine OptionalId to make it accept generic types #46412

Closed
wants to merge 1 commit into from

Conversation

g-pascal
Copy link

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Add or edit tests to reflect the change. (Run with npm test.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

#46375

This PR fixes the insert{,One,Many} methods for generic TSchema types, with the drawback of breaking type checking on the _id property in the OptionalId type.

  async function withConstrainedGeneric<T extends { _id: ObjectId }>(param: T) {
    const constrainedGenericCollection = db.collection<T>('testCollection');
    // TS2345: Argument of type 'T' is not assignable to parameter of type 'OptionalId<T>'.
    // Type '{ _id: ObjectId; }' is not assignable to type 'OptionalId<T>'.
    await constrainedGenericCollection.insertOne(param);
  }

  // Due to ExtractIdType not working with any generic
  function withConstrainedGeneric<T extends { _id: number }>(param: T) {
    // Type '0' is not assignable to type 'ExtractIdType '.
    const extracted: ExtractIdType<T> = 0;
  }
  • We lose the type-safety on the _id property in Collection.insert{,One,Many} methods with these changes
  type NumberId = { _id: number, stringField: string }
  const numberIdCollection = db.collection<NumberId>('testCollection');
  await numberIdCollection.insertOne({ stringField: '' }); // Should be an error: non-ObjectId _id must be defined
  await numberIdCollection.insertOne({ _id: '', stringField: '' }); // Should be an error: bad _id type
  • We also lose the type-safety on "any"-indexed types
  interface AnyIndexedType {
    stringField: string;
    [key: string]: any;
  }
  const indexTypeCollection1 = db.collection<AnyIndexedType>('testCollection');
  
  await indexTypeCollection1.insert({ stringField: 3 }); // Should be an error
  await indexTypeCollection1.insertOne({}); // Should be an error, stringField is required
  • The test-cases validating the _id property in insert{,One,Many} methods have been removed.
  • Added test-cases with a generic TSchema type.

- Before this commit, OptionalId was defined with a conditional type that could never resolve when given a generic.
- We lose the type-safety on the _id property in Collection.insert{,One,Many} methods with these changes, but we also fix a regression introduced when OptionalId was redefined.
- Remove the test-cases which were validating the _id property in insert{,One,Many} methods.
- Add some test-cases with an unconstrained and a constrained generic TSchema type.
@typescript-bot typescript-bot added the Popular package This PR affects a popular package (as counted by NPM download counts). label Jul 28, 2020
@typescript-bot typescript-bot added this to Waiting for Code Reviews in New Pull Request Status Board Jul 28, 2020
@typescript-bot
Copy link
Contributor

typescript-bot commented Jul 28, 2020

@g-pascal Thank you for submitting this PR! I see this is your first time submitting to DefinitelyTyped 👋 — I'm the local bot who will help you through the process of getting things through.

This is a live comment which I will keep updated.

1 package in this PR

Code Reviews

Because you edited one package and updated the tests (👏), I can help you merge this PR once someone else signs off on it.

Status

  • ❌ No merge conflicts
  • ✅ Continuous integration tests have passed
  • ❌ Most recent commit is approved by type definition owners or DT maintainers

Once every item on this list is checked, I'll ask you for permission to merge and publish the changes.

Inactive

This PR has been inactive for 31 days — it is considered abandoned!


Diagnostic Information: What the bot saw about this PR
{
  "type": "info",
  "now": "-",
  "pr_number": 46412,
  "author": "g-pascal",
  "owners": [
    "CaselIT",
    "alanmarcell",
    "dante-101",
    "mcortesi",
    "EnricoPicci",
    "AJCStriker",
    "julien-c",
    "daprahamian",
    "denys-bushulyak",
    "BastienAr",
    "sindbach",
    "geraldinelemeur",
    "various89",
    "angela-1",
    "hector7",
    "floric",
    "erikc5000",
    "Manc",
    "jloveridge",
    "ranguna",
    "HosseinAgha",
    "albertossilva",
    "peterblazejewicz",
    "LinusU",
    "taxilian",
    "xamgore",
    "avaly",
    "HitkoDev",
    "Celend",
    "jtassin"
  ],
  "dangerLevel": "ScopedAndTested",
  "headCommitAbbrOid": "d7fb443",
  "headCommitOid": "d7fb4431209a5913dac3f45a2b977c8c0ad5f027",
  "mergeIsRequested": false,
  "stalenessInDays": 31,
  "lastPushDate": "2020-07-28T16:59:01.000Z",
  "lastCommentDate": "2020-08-14T21:32:12.000Z",
  "maintainerBlessed": true,
  "reviewLink": "https://github.com/DefinitelyTyped/DefinitelyTyped/pull/46412/files",
  "hasMergeConflict": true,
  "authorIsOwner": false,
  "isFirstContribution": true,
  "popularityLevel": "Popular",
  "newPackages": [],
  "packages": [
    "mongodb"
  ],
  "files": [
    {
      "path": "types/mongodb/index.d.ts",
      "kind": "definition",
      "package": "mongodb"
    },
    {
      "path": "types/mongodb/test/collection/insertX.ts",
      "kind": "test",
      "package": "mongodb"
    }
  ],
  "hasDismissedReview": false,
  "ciResult": "pass",
  "reviewersWithStaleReviews": [],
  "approvalFlags": 0,
  "isChangesRequested": false
}

@typescript-bot
Copy link
Contributor

typescript-bot commented Jul 28, 2020

@danger-public
Copy link

Inspecting the JavaScript source for this package found some properties that are not in the .d.ts files.
The check for missing properties isn't always right, so take this list as advice, not a requirement.

mongodb (unpkg)

was missing the following properties:

  1. The declaration doesn't match the JavaScript module 'mongodb'. Reason:
    The JavaScript module can be called or constructed, but the declaration module cannot.

The most common way to resolve this error is to use 'export =' syntax.
To learn more about 'export =' syntax, see https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require.

  1. MongoTimeoutError
  2. MongoServerSelectionError
  3. MongoWriteConcernError
  4. MongoBulkWriteError
as well as these 11 other properties...

BulkWriteError, Admin, Collection, GridStore, Chunk, CoreServer, CoreConnection, Map, Symbol, BSONRegExp, instrument

Generated by 🚫 dangerJS against d7fb443

@typescript-bot
Copy link
Contributor

👋 Hi there! I’ve run some quick measurements against master and your PR. These metrics should help the humans reviewing this PR gauge whether it might negatively affect compile times or editor responsiveness for users who install these typings.

Let’s review the numbers, shall we?

Comparison details 📊
master #46412 diff
Batch compilation
Memory usage (MiB) 74.5 74.2 -0.4%
Type count 16048 15982 0%
Assignability cache size 3811 3758 -1%
Language service
Samples taken 1889 1852 -2%
Identifiers in tests 2234 2229 0%
getCompletionsAtPosition
    Mean duration (ms) 385.9 390.0 +1.1%
    Mean CV 8.0% 8.0%
    Worst duration (ms) 486.0 501.5 +3.2%
    Worst identifier replacement TestModel
getQuickInfoAtPosition
    Mean duration (ms) 384.6 389.6 +1.3%
    Mean CV 7.8% 7.9% +1.5%
    Worst duration (ms) 473.8 489.0 +3.2%
    Worst identifier find client

It looks like nothing changed too much. I won’t post performance data again unless it gets worse.

@typescript-bot typescript-bot added the Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance. label Jul 28, 2020
@typescript-bot typescript-bot added the Unmerged The author did not merge the PR when it was ready. label Aug 7, 2020
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Action in New Pull Request Status Board Aug 7, 2020
@andrewbranch andrewbranch moved this from Needs Maintainer Action to Waiting for Code Reviews in New Pull Request Status Board Aug 14, 2020
@typescript-bot typescript-bot removed the Unmerged The author did not merge the PR when it was ready. label Aug 14, 2020
@typescript-bot typescript-bot added the Unmerged The author did not merge the PR when it was ready. label Aug 24, 2020
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Action in New Pull Request Status Board Aug 24, 2020
@typescript-bot typescript-bot removed the Unmerged The author did not merge the PR when it was ready. label Aug 27, 2020
@typescript-bot typescript-bot moved this from Needs Maintainer Action to Waiting for Code Reviews in New Pull Request Status Board Aug 27, 2020
@typescript-bot typescript-bot added the Unmerged The author did not merge the PR when it was ready. label Aug 28, 2020
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Action in New Pull Request Status Board Aug 28, 2020
@typescript-bot typescript-bot removed the Unmerged The author did not merge the PR when it was ready. label Aug 28, 2020
@typescript-bot typescript-bot moved this from Needs Maintainer Action to Waiting for Code Reviews in New Pull Request Status Board Aug 28, 2020
@typescript-bot typescript-bot added the Abandoned This PR had no activity for a long time, and is considered abandoned label Sep 15, 2020
@typescript-bot typescript-bot moved this from Waiting for Code Reviews to Needs Maintainer Action in New Pull Request Status Board Sep 15, 2020
@sheetalkamat sheetalkamat moved this from Needs Maintainer Action to Waiting for Code Reviews in New Pull Request Status Board Sep 21, 2020
@typescript-bot typescript-bot removed the Abandoned This PR had no activity for a long time, and is considered abandoned label Sep 22, 2020
@typescript-bot typescript-bot added Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. Abandoned This PR had no activity for a long time, and is considered abandoned labels Oct 23, 2020
@typescript-bot typescript-bot removed this from Waiting for Code Reviews in New Pull Request Status Board Oct 23, 2020
@typescript-bot
Copy link
Contributor

@g-pascal Unfortunately, this pull request currently has a merge conflict 😥. Please update your PR branch to be up-to-date with respect to master. Have a nice day!

@typescript-bot
Copy link
Contributor

@g-pascal To keep things tidy, we have to close PRs that aren't mergeable and don't have activity in the last month. No worries, though — please open a new PR if you'd like to continue with this change. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Abandoned This PR had no activity for a long time, and is considered abandoned Has Merge Conflict This PR can't be merged because it has a merge conflict. The author needs to update it. Perf: Same typescript-bot determined that this PR will not significantly impact compilation performance. Popular package This PR affects a popular package (as counted by NPM download counts).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants