Skip to content

Commit 44bec19

Browse files
feat(NODE-3034): deprecate number as an input to ObjectId constructor (#640)
1 parent 3b2ed17 commit 44bec19

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/objectid.ts

+42-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,48 @@ export class ObjectId extends BSONValue {
4444
private __id?: string;
4545

4646
/**
47-
* Create an ObjectId type
47+
* Create ObjectId from a number.
4848
*
49-
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
49+
* @param inputId - A number.
50+
* @deprecated Instead, use `static createFromTime()` to set a numeric value for the new ObjectId.
51+
*/
52+
constructor(inputId: number);
53+
/**
54+
* Create ObjectId from a 24 character hex string.
55+
*
56+
* @param inputId - A 24 character hex string.
57+
*/
58+
constructor(inputId: string);
59+
/**
60+
* Create ObjectId from the BSON ObjectId type.
61+
*
62+
* @param inputId - The BSON ObjectId type.
63+
*/
64+
constructor(inputId: ObjectId);
65+
/**
66+
* Create ObjectId from the object type that has the toHexString method.
67+
*
68+
* @param inputId - The ObjectIdLike type.
69+
*/
70+
constructor(inputId: ObjectIdLike);
71+
/**
72+
* Create ObjectId from a 12 byte binary Buffer.
73+
*
74+
* @param inputId - A 12 byte binary Buffer.
75+
*/
76+
constructor(inputId: Uint8Array);
77+
/** To generate a new ObjectId, use ObjectId() with no argument. */
78+
constructor();
79+
/**
80+
* Implementation overload.
81+
*
82+
* @param inputId - All input types that are used in the constructor implementation.
83+
*/
84+
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array);
85+
/**
86+
* Create a new ObjectId.
87+
*
88+
* @param inputId - An input value to create a new ObjectId from.
5089
*/
5190
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array) {
5291
super();
@@ -65,7 +104,7 @@ export class ObjectId extends BSONValue {
65104
workingId = inputId;
66105
}
67106

68-
// the following cases use workingId to construct an ObjectId
107+
// The following cases use workingId to construct an ObjectId
69108
if (workingId == null || typeof workingId === 'number') {
70109
// The most common use case (blank id, new objectId instance)
71110
// Generate a new id

test/types/bson.test-d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expectType, expectError } from 'tsd';
1+
import { expectType, expectError , expectDeprecated, expectNotDeprecated } from 'tsd';
22
import {
33
Binary,
44
Code,
@@ -10,6 +10,7 @@ import {
1010
MaxKey,
1111
MinKey,
1212
ObjectId,
13+
ObjectIdLike,
1314
BSONRegExp,
1415
BSONSymbol,
1516
Timestamp,
@@ -77,3 +78,7 @@ expectType<'Binary'>(UUID.prototype._bsontype)
7778
declare const bsonValue: BSONValue;
7879
expectType<string>(bsonValue._bsontype);
7980
expectType<(depth?: number | undefined, options?: unknown, inspect?: InspectFn | undefined) => string>(bsonValue.inspect);
81+
82+
expectNotDeprecated(new ObjectId('foo'));
83+
expectDeprecated(new ObjectId(42));
84+
expectNotDeprecated(new ObjectId(42 as string | number));

0 commit comments

Comments
 (0)