Skip to content

Commit f2dc3ee

Browse files
author
Chris Cho
authored
DOCSP-20482: TypeScript Benefits (#663)
* DOCSP-20482: TypeScript Benefits
1 parent 0e03f8d commit f2dc3ee

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

source/fundamentals/typescript.txt

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
.. node-fundamentals-typescript:
2+
13
==========
24
TypeScript
35
==========
46

5-
.. default-domain:: mongodb
6-
77
.. contents:: On this page
88
:local:
99
:backlinks: none
@@ -17,6 +17,10 @@ In this guide, you can learn about the **TypeScript** features and limitations
1717
of the MongoDB Node.js driver. TypeScript is a strongly typed programming
1818
language that compiles to JavaScript.
1919

20+
The TypeScript compiler offers type checking in real time. Code editors that
21+
support TypeScript can provide autocomplete suggestions, display documentation
22+
inline, and identify type-related errors.
23+
2024
All TypeScript features of the driver are optional. All valid JavaScript
2125
code written with the driver is also valid TypeScript code.
2226

@@ -120,7 +124,7 @@ You can enable type-checking by constructing filters as ``StrictFilter`` or
120124
``StrictUpdateFilter`` types.
121125

122126
.. warning::
123-
127+
124128
The ``StrictFilter`` and ``StrictUpdateFilter`` types are experimental and
125129
may show type errors in valid queries where there should be none.
126130

@@ -139,7 +143,7 @@ filter. The {+driver-short+} reports a type error because the value of
139143
``classification.color`` is a boolean instead of a string.
140144

141145
.. code-block:: typescript
142-
146+
143147
const updateFilter: StrictUpdateFilter<ClassificationPet> = { $set: { "classification.color": false } }
144148
await pets.updateOne({}, updateFilter);
145149

@@ -162,7 +166,7 @@ each of which includes a ``time`` field:
162166
name: string;
163167
mealtimes: Mealtime[];
164168
}
165-
169+
166170
interface Mealtime{
167171
time: string;
168172
amount: number;
@@ -177,7 +181,7 @@ updates the nested ``time`` field of the ``Mealtime`` instance at index
177181
:emphasize-lines: 5
178182

179183
const mealCounter = 1;
180-
184+
181185
await myColl.findOneAndUpdate(
182186
{ name: "Lassie" },
183187
{ $set: { [`mealtimes.${mealCounter}.time` as const]: '4:00 PM' } },
@@ -197,9 +201,9 @@ section.
197201
Working with the _id Field
198202
--------------------------
199203

200-
MongoDB does not recommend specifying the ``_id`` as a part of your model.
201-
Omitting the ``_id`` field makes the model more generic and reusable and more accurately
202-
models the data important to an application. The Node driver’s TypeScript integration
204+
MongoDB does not recommend specifying the ``_id`` as a part of your model.
205+
Omitting the ``_id`` field makes the model more generic and reusable and more accurately
206+
models the data important to an application. The Node driver’s TypeScript integration
203207
takes care of adding the ``_id`` field to the return types for relevant methods.
204208

205209
If you need to work with the ``_id`` field in your models, see the below sections for
@@ -225,12 +229,12 @@ of insert operations. The following table describes how different
225229
* - | Unspecified
226230
- | Not applicable
227231
- | No
228-
- | The driver creates an
232+
- | The driver creates an
229233
:manual:`ObjectId </reference/method/ObjectId/>`
230234
value for each inserted document.
231235

232236
* - | Specified
233-
- | ``{ _id: number };``
237+
- | ``{ _id: number };``
234238
- | Yes
235239
- | If you do not specify a value for the ``_id`` field in an insert operation,
236240
the driver raises an error.
@@ -267,7 +271,7 @@ The following code uses the preceding interface along with the
267271

268272
const database = client.db("<your database>");
269273
const collection = db.collection<OptionalId<IdPet>>("<your collection>");
270-
274+
271275
myColl.insertOne({
272276
name: "Spot",
273277
age: 2
@@ -315,7 +319,7 @@ interface to return a document with an ``_id`` inferred to be of type ``ObjectId
315319

316320
const database = client.db("<your database>");
317321
const collection = db.collection<Pet>("<your collection>");
318-
322+
319323
const document = await myColl.findOne({
320324
name: "Spot",
321325
});
@@ -374,7 +378,7 @@ document with an ``_id`` inferred to be of type ``number``:
374378
);
375379
// Compile time error: Property '_id' does not exist on type 'ProjectedDocument'.
376380
console.log(doc._id.generationTime);
377-
381+
378382
To view a runnable TypeScript example that includes a find method applying a
379383
projection, see the
380384
:ref:`Find a Document <node-driver-findone-usage-example-code-snippet>` page.

0 commit comments

Comments
 (0)