Skip to content

(DOCSP-20071) Update TS Limitations #275

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

Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
70649c4
remove limitation and add admonition
biniona-mongodb Jan 11, 2022
fd76f80
wip
biniona-mongodb Jan 11, 2022
438156c
wip
biniona-mongodb Jan 11, 2022
42cae8c
wip
biniona-mongodb Jan 11, 2022
fc3537b
wip
biniona-mongodb Jan 11, 2022
1853bf5
grammar check, move to own section
biniona-mongodb Jan 11, 2022
ea95abc
proofread
biniona-mongodb Jan 11, 2022
b35c0d9
CC - edits part 1
biniona-mongodb Jan 12, 2022
a758cdd
cc - break recursive example intro into sentence
biniona-mongodb Jan 12, 2022
4ba4c20
typo
biniona-mongodb Jan 12, 2022
47ec306
consistency
biniona-mongodb Jan 12, 2022
079cd5e
cc - edits
biniona-mongodb Jan 12, 2022
1be07ca
update subheadings
biniona-mongodb Jan 12, 2022
f61c9f1
test
biniona-mongodb Jan 12, 2022
7e36683
Revert "test"
biniona-mongodb Jan 12, 2022
f2ec69e
any -> all
biniona-mongodb Jan 12, 2022
eb77c53
cc - discussion
biniona-mongodb Jan 12, 2022
2d3cc37
edits
biniona-mongodb Jan 12, 2022
b48748e
warning
biniona-mongodb Jan 12, 2022
e1648d0
cc - edits
biniona-mongodb Jan 13, 2022
ad6c58d
Merge branch 'master' into DOCSP-20071-Support-Dot-Notation-TS
biniona-mongodb Jan 13, 2022
f45b0a8
update limitations for Node PR #3102
biniona-mongodb Jan 18, 2022
fe64c9f
typo
biniona-mongodb Jan 18, 2022
353416a
proofread
biniona-mongodb Jan 18, 2022
4bfa35e
wip
biniona-mongodb Jan 18, 2022
f7967df
grammar-check
biniona-mongodb Jan 18, 2022
e3f65ec
updates from slack thread
biniona-mongodb Jan 19, 2022
8bd69e4
proofread
biniona-mongodb Jan 19, 2022
06de329
proofread
biniona-mongodb Jan 19, 2022
8fb218b
cc - edits
biniona-mongodb Jan 20, 2022
6276c5d
proofread, spell check, grammar check
biniona-mongodb Jan 20, 2022
36730a5
cc - edits
biniona-mongodb Jan 24, 2022
67dc810
move header out of include to avoid h3
biniona-mongodb Jan 24, 2022
cc4fdb1
tweak
biniona-mongodb Jan 24, 2022
ad3b47e
dp - edits
biniona-mongodb Jan 31, 2022
59ed5e0
tweaks
biniona-mongodb Jan 31, 2022
7a1748f
proofread
biniona-mongodb Jan 31, 2022
8c97f31
tweak
biniona-mongodb Jan 31, 2022
11ba220
tweak example as genus and family are not independent
biniona-mongodb Jan 31, 2022
5a41582
dp - edit
biniona-mongodb Jan 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pgp-version = "{+version+}"
api = "https://mongodb.github.io/node-mongodb-native/{+version+}"
mongosh = "``mongosh``"
driver = "node"
driver-long = "MongoDB Node.js driver"
driver-short = "Node.js driver"
4 changes: 2 additions & 2 deletions source/code-snippets/typescript/dot-notation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface TestNumber {
myNumber: number;
}

const database = client.db("<your db>");
const collection = db.collection<TestNumber>("...");
const database = client.db("<your database>");
const collection = db.collection<TestNumber>("<your collection>");
collection.find({ someRandomKey: "Accepts any type!" });
// end-no-key
111 changes: 36 additions & 75 deletions source/fundamentals/typescript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,21 @@ Any object type can extend the ``Document`` interface.
For more information on object types, see the
`TypeScript handbook <https://www.typescriptlang.org/docs/handbook/2/objects.html>`__.

Extend Document
~~~~~~~~~~~~~~~
Parameters that Extend Document
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following classes accept any type that extends the ``Document``
interface:
The following classes accept all non-recursive types that extend
the ``Document`` interface:

- `Collection <{+api+}/classes/Collection.html>`__
- `ChangeStream <{+api+}/classes/ChangeStream.html>`__

To view an example of a recursive type, see the :ref:`<node-driver-limitations>` section.

You can pass a type parameter that extends the ``Document`` interface like this:

.. _mongodb-node-typescript-pet-interface:

.. literalinclude:: /code-snippets/typescript/extend-document.ts
:language: typescript
:linenos:
Expand All @@ -67,93 +71,50 @@ You can pass a type parameter that extends the ``Document`` interface like this:
:start-after: start-no-key
:end-before: end-no-key

Any Type
~~~~~~~~
Parameters of Any Type
~~~~~~~~~~~~~~~~~~~~~~

The following classes accept any type parameter:
The following classes accept all type parameters that are not recursive:

- `FindCursor <{+api+}/classes/FindCursor.html>`__
- `AggregationCursor <{+api+}/classes/AggregationCursor.html>`__

To view an example of a recursive type, see the :ref:`<node-driver-limitations>` section.

You can find a code snippet that shows how to specify a type for the ``FindCursor``
class in the
:ref:`Find Multiple Documents Usage Example <node-driver-find-usage-example-code-snippet>`.

Limitations
-----------

.. _node-driver-typescript-limitations-dot-notation:

The driver cannot infer the type of values with keys containing **dot
notation**. Dot notation is a property access syntax for navigating BSON objects.
Click on the tabs to see code snippets that highlight this behavior:

.. tabs::

.. tab:: Dot Notation
:tabid: dot-notation

The following code snippet does not raise a type error:

.. literalinclude:: /code-snippets/typescript/dot-notation.ts
:language: typescript
:linenos:
:start-after: start-no-error
:end-before: end-no-error

.. tab:: Nested Objects
:tabid: nested-objects

The following code snippet raises a type error:

.. literalinclude:: /code-snippets/typescript/dot-notation.ts
:language: typescript
:linenos:
:start-after: start-error
:end-before: end-error
.. _node-driver-limitations:

This is the error:
Limitations For Driver Version {+version+}
----------------------------------

.. code-block:: text
You cannot specify a recursive type as a type parameter in version {+version+} of the driver.

Type 'string' is not assignable to type 'number'.
If you specify a recursive type, the driver raises the following error:

Despite the lack of type safety, we still recommend that you use dot notation to
access nested fields in query and update documents when you use TypeScript. You
must manually check that your nested field values have your intended type.
.. code-block:: text

.. note:: Reason To Use Dot Notation
error TS2615: Type of property 'r' circularly references itself in mapped type '{ [Key in keyof RecursiveType]: [Key, ...NestedPaths<RecursiveType[Key]>]; }'.

In the MongoDB Query Language, you must match a subdocument exactly
when specifying subdocuments in a query. Dot notation allows you to query
nested fields without matching subdocuments exactly.
A recursive type is a type that references itself. You can update
the :ref:`Pet <mongodb-node-typescript-pet-interface>` interface
to be recursive by adding the following field:

To show this behavior, lets say you have a collection containing
only the following document:

.. code-block:: json

{ field: { s1: "hi", s2: "bye" } }

The following query returns no results from this collection, as the value of
``field`` does not exactly match ``{ s1: "hi" }``:

.. literalinclude:: /code-snippets/typescript/note-on-dot-notation.ts
:language: typescript
:linenos:
:start-after: start-no-doc
:end-before: end-no-doc

The following queries both return your document:
.. code-block:: typescript
:emphasize-lines: 2

.. literalinclude:: /code-snippets/typescript/note-on-dot-notation.ts
:language: typescript
:linenos:
:start-after: start-doc
:end-before: end-doc
interface Pet {
pet: Pet | null;
name: string;
age: number;
cute: true;
}

The syntax of the query that does not use dot notation is cumbersome and hard
to understand, and may not be worth the type safety obtained from
avoiding dot notation.
To track the fix for this limitation, see
`NODE-3852 <https://jira.mongodb.org/browse/NODE-3852>`__
in JIRA issue tracker.

For more information on dot notation, see :manual:`the MongoDB Manual </core/document/#dot-notation>`.
If you must apply a recursive type to your documents, use version 4.2 of
the {+driver-long+}.
6 changes: 0 additions & 6 deletions source/usage-examples/bulkWrite.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ to ``bulkWrite()`` includes examples of ``insertOne``, ``updateMany``, and
:language: typescript
:linenos:

.. important:: Dot Notation Loses Type Safety

You lose type-safety for values when you use dot notation in keys. For
more information, see our guide on
:ref:`TypeScript in the driver <node-driver-typescript-limitations-dot-notation>`.

When you run the preceding example, you should see the following output:

.. code-block:: javascript
Expand Down