@@ -84,13 +84,17 @@ You can find a code snippet that shows how to specify a type for the ``FindCurso
84
84
class in the
85
85
:ref:`Find Multiple Documents Usage Example <node-driver-find-usage-example-code-snippet>`.
86
86
87
+ .. _node-ts-type-safety:
88
+
87
89
Type Safety and Dot Notation
88
90
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89
91
90
- If you specify a query or update with **dot notation**, the {+driver-short+}
91
- provides type safety if your query or update does not
92
- :ref:`reference a nested instance of a recursive type <node-driver-recursive-types-dot-notation>`.
93
- Dot notation is a syntax you can use to navigate nested JSON objects.
92
+ Starting in version 5.0, by default, the {+driver-short+} does not provide type
93
+ safety for operations that search on fields expressed in **dot notation**. Dot
94
+ notation is a syntax you can use to navigate nested JSON objects. When
95
+ you construct a filter to pass to a query, the driver will not raise a
96
+ type error even if you specify an incorrectly typed value for a field expressed
97
+ in dot notation.
94
98
95
99
The following code snippet defines the ``ClassificationPet`` interface,
96
100
which includes a ``classification`` field that enables you to specify the
@@ -104,22 +108,40 @@ genus and color of dogs and cats:
104
108
classification: { genus: "Canis" | "Felis"; color: string };
105
109
}
106
110
107
- The following code snippet correctly raises a type error when specifying
108
- the genus of an unsupported animal in a query:
111
+ The driver does not raise a type error for the following code sample,
112
+ even though the value of ``classification.color`` is a boolean
113
+ instead of a string:
109
114
110
115
.. code-block:: typescript
111
116
112
- database
113
- .collection<ClassificationPet>("<your collection>")
114
- .find({ "classification.genus": "Sylvilagus" });
117
+ await collection.findOneAndDelete({ "classification.color": false })
118
+
119
+ You can enable type-checking by constructing filters as ``StrictFilter`` or
120
+ ``StrictUpdateFilter`` types.
115
121
116
- The type error raised by the preceding code snippet is as follows:
122
+ .. warning::
123
+
124
+ The ``StrictFilter`` and ``StrictUpdateFilter`` types are experimental and
125
+ may show type errors in valid queries where there should be none.
117
126
118
- .. code-block:: none
127
+ In the following code sample, the filter is assigned a
128
+ ``StrictFilter`` type. Given this filter type, the {+driver-short+}
129
+ reports a type error because the value of ``classification.color`` is a
130
+ boolean instead of a string.
131
+
132
+ .. code-block:: typescript
119
133
120
- No overload matches this call.
121
- ...
122
- Type '"Sylvilagus"' is not assignable to type 'Condition<"Canis" | "Felis">'.
134
+ const filterPredicate: StrictFilter<ClassificationPet> = { "classification.color": false };
135
+ await collection.findOneAndDelete(filterPredicate);
136
+
137
+ The following example assigns a ``StrictUpdateFilter`` type to an update
138
+ filter. The {+driver-short+} reports a type error because the value of
139
+ ``classification.color`` is a boolean instead of a string.
140
+
141
+ .. code-block:: typescript
142
+
143
+ const updateFilter: StrictUpdateFilter<ClassificationPet> = { $set: { "classification.color": false } }
144
+ await pets.updateOne({}, updateFilter);
123
145
124
146
Referencing Keys that Incorporate Variables
125
147
```````````````````````````````````````````
0 commit comments