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 11 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"
2 changes: 1 addition & 1 deletion 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 database = client.db("<your database>");
const collection = db.collection<TestNumber>("...");
collection.find({ someRandomKey: "Accepts any type!" });
// end-no-key
97 changes: 30 additions & 67 deletions source/fundamentals/typescript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ For more information on object types, see the
Extend Document
~~~~~~~~~~~~~~~

The following classes accept any type that extends the ``Document``
interface:
The following classes accept all types that extends the ``Document`` interface
and that are not recursive:

- `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:

.. literalinclude:: /code-snippets/typescript/extend-document.ts
Expand All @@ -70,90 +72,51 @@ You can pass a type parameter that extends the ``Document`` interface like this:
Any Type
~~~~~~~~

The following classes accept any type parameter:
The following classes accept any type parameter that is 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>`.

.. _node-driver-limitations:

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
You cannot specify a recursive type as a type parameter for {+driver-long+}
classes. The following code snippet defines a recursive type:

This is the error:

.. code-block:: text

Type 'string' is not assignable to type 'number'.

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.

.. note:: Reason To Use Dot Notation
.. code-block:: typescript

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.
interface RecursiveType {
r: RecursiveType | BaseCase;
i: number;
}

To show this behavior, lets say you have a collection containing
only the following document:
type BaseCase = "done";

.. code-block:: json
The following code snippet uses the preceding recursive type:

{ field: { s1: "hi", s2: "bye" } }
.. code-block:: typescript

The following query returns no results from this collection, as the value of
``field`` does not exactly match ``{ s1: "hi" }``:
const database = client.db("<your database>");
const col = database.collection<RecursiveType>("<your collection>");
const document = await col.findOne({ i: 1 });

.. literalinclude:: /code-snippets/typescript/note-on-dot-notation.ts
:language: typescript
:linenos:
:start-after: start-no-doc
:end-before: end-no-doc
The preceding code snippet raises the following error at compile time:

The following queries both return your document:
.. code-block:: text

.. literalinclude:: /code-snippets/typescript/note-on-dot-notation.ts
:language: typescript
:linenos:
:start-after: start-doc
:end-before: end-doc
error TS2615: Type of property 'r' circularly references itself in mapped type '{ [Key in keyof RecursiveType]: [Key, ...NestedPaths<RecursiveType[Key]>]; }'.

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.
If you must use a recursive type as your document schema, use version 4.2 of
the {+driver-long+}.

For more information on dot notation, see :manual:`the MongoDB Manual </core/document/#dot-notation>`.
To track the fix for this limitation, see
`NODE-3852 <https://jira.mongodb.org/browse/NODE-3852>`__
in JIRA.
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