|
1 | 1 | Learn about the following TypeScript specific limitations of the {+driver-short+}: |
2 | 2 |
|
3 | 3 | - :ref:`No type safety for dot notation references to nested instances of recursive types <node-driver-recursive-types-dot-notation>` |
4 | | -- :ref:`No mutually recursive types <node-driver-limitations-mutual-recursion>` |
| 4 | +- :ref:`Depth limitations on type safety for mutually recursive types <node-driver-limitations-mutual-recursion>` |
5 | 5 |
|
6 | 6 | .. _node-driver-recursive-types-dot-notation: |
7 | 7 |
|
8 | 8 | Recursive Types and Dot Notation |
9 | 9 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
10 | 10 |
|
11 | | -.. important:: Impacted Versions |
12 | | - |
13 | | - - 4.3 |
14 | | - - 4.4 |
15 | | - |
16 | | -The {+driver-short+} cannot provide type safety within nested instances of |
| 11 | +The {+driver-short+} cannot provide type safety within nested instances of |
17 | 12 | **recursive types** referenced through dot notation. |
18 | 13 |
|
19 | 14 | A recursive type is a type that references itself. You can update |
@@ -80,40 +75,54 @@ in the MongoDB manual. |
80 | 75 | Mutual Recursion |
81 | 76 | ~~~~~~~~~~~~~~~~ |
82 | 77 |
|
83 | | -.. important:: Impacted Versions |
84 | | - |
85 | | - - 4.3 |
86 | | - - 4.4 |
87 | | - |
88 | | -You cannot specify a **mutually recursive** type as a type parameter. |
89 | | - |
90 | | -A mutually recursive type exists when two types contain a property that is of |
91 | | -the other's type. You can update the |
92 | | -:ref:`Pet <mongodb-node-typescript-pet-interface>` interface |
93 | | -to be mutually recursive by allowing a pet to have a handler, and defining a |
94 | | -handler to have a pet. The following are the mutually |
| 78 | +A **mutually recursive** type exists when two types contain a property that is of |
| 79 | +the other's type. You can update the :ref:`Pet <mongodb-node-typescript-pet-interface>` |
| 80 | +interface to be mutually recursive by allowing a pet to have a handler, and |
| 81 | +defining a handler to have a pet. The following examples reference the mutually |
95 | 82 | recursive ``Pet`` and ``Handler`` interfaces: |
96 | 83 |
|
97 | 84 | .. code-block:: typescript |
98 | 85 | :emphasize-lines: 2, 8 |
99 | 86 |
|
100 | | - interface MutuallyRecursivePet { |
| 87 | + interface Pet { |
101 | 88 | handler?: Handler; |
102 | 89 | name: string; |
103 | 90 | age: number; |
104 | 91 | } |
105 | 92 |
|
106 | 93 | interface Handler { |
107 | | - pet: MutuallyRecursivePet; |
| 94 | + pet: Pet; |
108 | 95 | name: string; |
109 | 96 | } |
| 97 | + |
| 98 | +The {+driver-short+} provides type safety for mutually recursive types |
| 99 | +referenced through dot notation up to a depth of eight. The following code |
| 100 | +snippet assigns a ``string`` to a ``number`` and raises a type error because |
| 101 | +the referenced property is at a depth of four: |
| 102 | + |
| 103 | +.. code-block:: typescript |
| 104 | + :emphasize-lines: 3 |
| 105 | +
|
| 106 | + database |
| 107 | + .collection<Pet>("<your collection>") |
| 108 | + .findOne({'handler.pet.handler.pet.age': "four"}); |
110 | 109 |
|
111 | | -If you specify a mutually recursive type, the TypeScript compiler raises the |
112 | | -following error: |
| 110 | +The error raised by the preceding code snippet is as follows: |
113 | 111 |
|
114 | 112 | .. code-block:: none |
115 | 113 |
|
116 | | - error TS2615: Type of property 'r' circularly references itself in mapped type '{ [Key in keyof MutuallyRecursive]... |
| 114 | + index.ts(19,59): error TS2769: No overload matches this call. |
| 115 | + The last overload gave the following error. |
| 116 | + Type 'string' is not assignable to type 'Condition<number> | undefined'. |
| 117 | +
|
| 118 | +At a depth greater than or equal to eight, TypeScript compiles your code but no |
| 119 | +longer type checks it. The following code assigns a ``string`` to a ``number`` |
| 120 | +property but does not cause a compilation error because the referenced property |
| 121 | +is at a depth of 10: |
117 | 122 |
|
118 | | -If you must apply a mutually recursive type to your classes, use version 4.2 of |
119 | | -the {+driver-short+}. |
| 123 | +.. code-block:: typescript |
| 124 | + :emphasize-lines: 3 |
| 125 | +
|
| 126 | + database |
| 127 | + .collection<Pet>("<your collection>") |
| 128 | + .findOne({'handler.pet.handler.pet.handler.pet.handler.pet.handler.pet.age': "four"}); |
0 commit comments