Skip to content

Commit a185b2b

Browse files
author
Chris Cho
committed
DOCSP-20482: TypeScript Benefits (#663)
* DOCSP-20482: TypeScript Benefits (cherry picked from commit f2dc3ee)
1 parent 0181962 commit a185b2b

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

source/fundamentals/typescript.txt

Lines changed: 17 additions & 13 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

@@ -140,7 +144,7 @@ each of which includes a ``time`` field:
140144
name: string;
141145
mealtimes: Mealtime[];
142146
}
143-
147+
144148
interface Mealtime{
145149
time: string;
146150
amount: number;
@@ -155,7 +159,7 @@ updates the nested ``time`` field of the ``Mealtime`` instance at index
155159
:emphasize-lines: 5
156160

157161
const mealCounter = 1;
158-
162+
159163
await collection.findOneAndUpdate(
160164
{ name: "Lassie" },
161165
{ $set: { [`mealtimes.${mealCounter}.time` as const]: '4:00 PM' } },
@@ -175,9 +179,9 @@ section.
175179
Working with the _id Field
176180
--------------------------
177181

178-
MongoDB does not recommend specifying the ``_id`` as a part of your model.
179-
Omitting the ``_id`` field makes the model more generic and reusable and more accurately
180-
models the data important to an application. The Node driver’s TypeScript integration
182+
MongoDB does not recommend specifying the ``_id`` as a part of your model.
183+
Omitting the ``_id`` field makes the model more generic and reusable and more accurately
184+
models the data important to an application. The Node driver’s TypeScript integration
181185
takes care of adding the ``_id`` field to the return types for relevant methods.
182186

183187
If you need to work with the ``_id`` field in your models, see the below sections for
@@ -203,12 +207,12 @@ of insert operations. The following table describes how different
203207
* - | Unspecified
204208
- | Not applicable
205209
- | No
206-
- | The driver creates an
210+
- | The driver creates an
207211
:manual:`ObjectId </reference/method/ObjectId/>`
208212
value for each inserted document.
209213

210214
* - | Specified
211-
- | ``{ _id: number };``
215+
- | ``{ _id: number };``
212216
- | Yes
213217
- | If you do not specify a value for the ``_id`` field in an insert operation,
214218
the driver raises an error.
@@ -245,7 +249,7 @@ The following code uses the preceding interface along with the
245249

246250
const database = client.db("<your database>");
247251
const collection = db.collection<OptionalId<IdPet>>("<your collection>");
248-
252+
249253
collection.insertOne({
250254
name: "Spot",
251255
age: 2
@@ -276,7 +280,7 @@ from the method is of the type specified in the schema.
276280

277281
However, if the type parameter you passed to your ``Collection`` instance does not
278282
include the ``_id`` field in its schema, the driver infers that the type of the
279-
``_id`` field returned from the method is ``ObjectId``.
283+
``_id`` field returned from the method is ``ObjectId``.
280284

281285
.. tip::
282286

@@ -293,7 +297,7 @@ interface to return a document with an ``_id`` inferred to be of type ``ObjectId
293297

294298
const database = client.db("<your database>");
295299
const collection = db.collection<Pet>("<your collection>");
296-
300+
297301
const document = await collection.findOne({
298302
name: "Spot",
299303
});
@@ -352,7 +356,7 @@ document with an ``_id`` inferred to be of type ``number``:
352356
);
353357
// Compile time error: Property '_id' does not exist on type 'ProjectedDocument'.
354358
console.log(doc._id.generationTime);
355-
359+
356360
To view a runnable TypeScript example that includes a find method applying a
357361
projection, see the
358362
:ref:`Find a Document <node-driver-findone-usage-example-code-snippet>` page.

0 commit comments

Comments
 (0)