-
Notifications
You must be signed in to change notification settings - Fork 53
DOCSP-25723 Updates to Mutually Recursive Schema Types #459
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 all commits
91e0f0f
c003c63
041baff
f65113a
da3bbe3
3707291
45b2fb1
b454952
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 |
---|---|---|
@@ -1,19 +1,14 @@ | ||
Learn about the following TypeScript specific limitations of the {+driver-short+}: | ||
|
||
- :ref:`No type safety for dot notation references to nested instances of recursive types <node-driver-recursive-types-dot-notation>` | ||
- :ref:`No mutually recursive types <node-driver-limitations-mutual-recursion>` | ||
- :ref:`Depth limitations on type safety for mutually recursive types <node-driver-limitations-mutual-recursion>` | ||
|
||
.. _node-driver-recursive-types-dot-notation: | ||
|
||
Recursive Types and Dot Notation | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. important:: Impacted Versions | ||
|
||
- 4.3 | ||
- 4.4 | ||
|
||
The {+driver-short+} cannot provide type safety within nested instances of | ||
The {+driver-short+} cannot provide type safety within nested instances of | ||
**recursive types** referenced through dot notation. | ||
|
||
A recursive type is a type that references itself. You can update | ||
|
@@ -80,40 +75,54 @@ in the MongoDB manual. | |
Mutual Recursion | ||
~~~~~~~~~~~~~~~~ | ||
|
||
.. important:: Impacted Versions | ||
|
||
- 4.3 | ||
- 4.4 | ||
|
||
You cannot specify a **mutually recursive** type as a type parameter. | ||
|
||
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 are the mutually | ||
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 examples reference the mutually | ||
recursive ``Pet`` and ``Handler`` interfaces: | ||
|
||
.. code-block:: typescript | ||
:emphasize-lines: 2, 8 | ||
|
||
interface MutuallyRecursivePet { | ||
interface Pet { | ||
ajhuh-mdb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
handler?: Handler; | ||
name: string; | ||
age: number; | ||
} | ||
|
||
interface Handler { | ||
pet: MutuallyRecursivePet; | ||
pet: Pet; | ||
name: string; | ||
} | ||
|
||
The {+driver-short+} provides type safety for mutually recursive types | ||
referenced through dot notation up to a depth of eight. The following code | ||
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. 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. Definitely worth confirming with the devs 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. The tests are correct, I'll be updating the notes |
||
snippet assigns a ``string`` to a ``number`` and raises a type error because | ||
the referenced property is at a depth of four: | ||
|
||
.. code-block:: typescript | ||
:emphasize-lines: 3 | ||
|
||
database | ||
.collection<Pet>("<your collection>") | ||
.findOne({'handler.pet.handler.pet.age': "four"}); | ||
|
||
If you specify a mutually recursive type, the TypeScript compiler raises the | ||
following error: | ||
The error raised by the preceding code snippet is as follows: | ||
|
||
.. code-block:: none | ||
|
||
error TS2615: Type of property 'r' circularly references itself in mapped type '{ [Key in keyof MutuallyRecursive]... | ||
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<number> | undefined'. | ||
|
||
At a depth greater than or equal to eight, TypeScript compiles your code but no | ||
longer type checks it. The following code assigns a ``string`` to a ``number`` | ||
property but does not cause a compilation error because the referenced property | ||
is at a depth of 10: | ||
|
||
If you must apply a mutually recursive type to your classes, use version 4.2 of | ||
the {+driver-short+}. | ||
.. code-block:: typescript | ||
:emphasize-lines: 3 | ||
|
||
database | ||
.collection<Pet>("<your collection>") | ||
.findOne({'handler.pet.handler.pet.handler.pet.handler.pet.handler.pet.age': "four"}); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed this admonition since this affects nested recursive types from all versions after v4.3.1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: I thought v4.11 fixed this issue. What am I missing?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah so the "fix" I meant to refer to when we met was not type safety.
For clarification, the "issue" from 4.3.0 was that, previously, just the presence of a self-referential recursive schema type at all would cause a compilation error. The "fix" in 4.3.1 is that they allowed for the code to compile with a recursive schema type present, but their solution, in doing so, couldn't guarantee type checking (which becomes a separate "issue" from the one originally mentioned).
Sorry about that confusion 😅