-
Notifications
You must be signed in to change notification settings - Fork 52
(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
Changes from 31 commits
70649c4
fd76f80
438156c
42cae8c
fc3537b
1853bf5
ea95abc
b35c0d9
a758cdd
4ba4c20
47ec306
079cd5e
1be07ca
f61c9f1
7e36683
f2ec69e
eb77c53
2d3cc37
b48748e
e1648d0
ad6c58d
f45b0a8
fe64c9f
353416a
4bfa35e
f7967df
e3f65ec
8bd69e4
06de329
8fb218b
6276c5d
36730a5
67dc810
cc4fdb1
ad3b47e
59ed5e0
7a1748f
8c97f31
11ba220
5a41582
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,11 @@ const collection = database.collection<TestType>("<your collection>"); | |
await collection.updateOne({}, { $set: { field: { nested: "A string" } } }); | ||
// end-error | ||
// start-no-key | ||
interface TestNumber { | ||
myNumber: number; | ||
interface User { | ||
email: string; | ||
} | ||
|
||
const database = client.db("<your db>"); | ||
const collection = db.collection<TestNumber>("..."); | ||
collection.find({ someRandomKey: "Accepts any type!" }); | ||
const database = client.db("<your database>"); | ||
const collection = db.collection<User>("<your collection>"); | ||
collection.find({ age: "Accepts any type!" }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a note that this will probably need to change soon to something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Daria! There is a ticket to add the "query nested fields without dot notation" example back to a different section of the documentation (DevED-DBX agreed that TS page seemed only tangentially related to dot notation and was not the correct place for this content). The page that should explain to readers how to use Dot Notation is the Update Arrays in a Document Page (linking to Java), however it seems like the corresponding Node page does not explicitly discuss dot notation. I've created a ticket to add this content . As this PR has already been through a few internal reviews and has grown to be fairly large, I think it is best to close out this pull request and handle the upcoming changes as part of a new task (any substantial additions will require an additional copy review which will take a decent amount of time as the diff for this PR has become pretty big). Sorry for the long response! Let me know if I have failed to address any of your concerns. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with doing the $-prefix updates later, but I really feel like the current page could benefit from a working example of dot notation type hinting use (i.e., show how having the wrong type in a non-recursive collection definition raises a typescript error) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I agree an example of working dot notation type checking would benefit readers. I've added a "dot notation" subsection to the Features section Let me know if this addresses your concerns! |
||
// end-no-key |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
interface Pet { | ||
name: string; | ||
age: number; | ||
cute: true; | ||
} | ||
|
||
const database = client.db("<your database>"); | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
Learn about the following TypeScript specific limitations of | ||
version {+version+} of the {+driver-short+}: | ||
|
||
- :ref:`The {+driver-short+} is unable to provide type safety for references to nested instances of recursive types specified through dot notation. <node-driver-recursive-types-dot-notation>` | ||
- :ref:`The {+driver-short+} does not support mutual recursion. <node-driver-limitations-mutual-recursion>` | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The TypeScript specific limitations of the {+driver-short+} relate to **recursive types**. | ||
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 allowing a pet to have its own pet. The following is the | ||
recursive ``Pet`` interface: | ||
|
||
.. _node-driver-limitations-recursive-pet: | ||
|
||
.. code-block:: typescript | ||
:emphasize-lines: 2 | ||
|
||
interface RecursivePet { | ||
pet?: RecursivePet; | ||
name: string; | ||
age: number; | ||
} | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. _node-driver-recursive-types-dot-notation: | ||
|
||
Recursive Types and Dot Notation | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The {+driver-short+} cannot provide type safety within nested instances of | ||
recursive types referenced through **dot notation**. Dot notation is a | ||
syntax you can use to navigate nested JSON objects. | ||
|
||
.. note:: Depth Limit | ||
|
||
The {+driver-short+} does not traverse nested recursive types when | ||
type checking dot notation keys to avoid hitting | ||
TypeScript's recursive depth limit. | ||
|
||
The following code snippet references a nested instance of the | ||
:ref:`RecursivePet <node-driver-limitations-recursive-pet>` interface | ||
with an incorrect type using dot notation, but the TypeScript compiler | ||
does not raise an error: | ||
|
||
.. code-block:: typescript | ||
:emphasize-lines: 3 | ||
|
||
database | ||
.collection<RecursivePet>("<your collection>") | ||
.findOne({ "pet.age": "Spot" }); | ||
|
||
The following code snippet references a top-level instance of the | ||
``RecursivePet`` interface with an incorrect type and raises a type error: | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: typescript | ||
:emphasize-lines: 3 | ||
|
||
database | ||
.collection<RecursivePet>("your-collection") | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.findOne({ pet: "Spot" }); | ||
|
||
The error raised by the preceding code snippet is as follows: | ||
|
||
.. code-block:: text | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
index.ts(19,59): error TS2769: No overload matches this call. | ||
The last overload gave the following error. | ||
Type 'string' is not assignable to type 'Condition<Pet>'. | ||
|
||
If you must have type safety within nested instances of recursive types, | ||
you must write your query or update without dot notation. | ||
|
||
To learn more about dot notation, see | ||
:manual:`Dot Notation </core/document/#dot-notation>` | ||
in the MongoDB manual. | ||
|
||
.. _node-driver-limitations-mutual-recursion: | ||
|
||
Mutual Recursion | ||
~~~~~~~~~~~~~~~~ | ||
|
||
You cannot specify a mutually recursive type as a type parameter in version | ||
{+version+} of the driver. | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
If you specify a mutually recursive type, the TypeScript compiler raises the | ||
following error: | ||
|
||
.. code-block:: text | ||
|
||
error TS2615: Type of property 'r' circularly references itself in mapped type '{ [Key in keyof MutuallyRecursive]... | ||
|
||
A mutually recursive type exists when two types contain a property that is of | ||
the other's type. You can update the | ||
:ref:`Pet <mongodb-node-typescript-pet-interface>` interface | ||
to be mutually recursive by allowing a pet to have a handler, and defining a | ||
handler to have a pet. The following is the mutually | ||
recursive ``Pet`` interface: | ||
biniona-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: typescript | ||
:emphasize-lines: 2, 8 | ||
|
||
interface MutuallyRecursivePet { | ||
handler?: Handler; | ||
name: string; | ||
age: number; | ||
} | ||
|
||
interface Handler { | ||
pet: MutuallyRecursivePet; | ||
name: string; | ||
} | ||
|
||
If you must apply a mutually recursive type to your classes, use version 4.2 of | ||
the {+driver-short+}. |
Uh oh!
There was an error while loading. Please reload this page.