Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/objectid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,48 @@ export class ObjectId extends BSONValue {
private __id?: string;

/**
* Create an ObjectId type
* Create ObjectId from a number.
*
* @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
* @param inputId - A number.
* @deprecated Instead, use `static createFromTime()` to set a numeric value for the new ObjectId.
*/
constructor(inputId: number);
/**
* Create ObjectId from a 24 character hex string.
*
* @param inputId - A 24 character hex string.
*/
constructor(inputId: string);
/**
* Create ObjectId from the BSON ObjectId type.
*
* @param inputId - The BSON ObjectId type.
*/
constructor(inputId: ObjectId);
/**
* Create ObjectId from the object type that has the toHexString method.
*
* @param inputId - The ObjectIdLike type.
*/
constructor(inputId: ObjectIdLike);
/**
* Create ObjectId from a 12 byte binary Buffer.
*
* @param inputId - A 12 byte binary Buffer.
*/
constructor(inputId: Uint8Array);
/** To generate a new ObjectId, use ObjectId() with no argument. */
constructor();
/**
* Implementation overload.
*
* @param inputId - All input types that are used in the constructor implementation.
*/
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array);
/**
* Create a new ObjectId.
*
* @param inputId - An input value to create a new ObjectId from.
*/
Comment thread
nbbeeken marked this conversation as resolved.
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array) {
super();
Expand All @@ -65,7 +104,7 @@ export class ObjectId extends BSONValue {
workingId = inputId;
}

// the following cases use workingId to construct an ObjectId
// The following cases use workingId to construct an ObjectId
if (workingId == null || typeof workingId === 'number') {
// The most common use case (blank id, new objectId instance)
// Generate a new id
Expand Down
7 changes: 6 additions & 1 deletion test/types/bson.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectType, expectError } from 'tsd';
import { expectType, expectError , expectDeprecated, expectNotDeprecated } from 'tsd';
import {
Binary,
Code,
Expand All @@ -10,6 +10,7 @@ import {
MaxKey,
MinKey,
ObjectId,
ObjectIdLike,
BSONRegExp,
BSONSymbol,
Timestamp,
Expand Down Expand Up @@ -77,3 +78,7 @@ expectType<'Binary'>(UUID.prototype._bsontype)
declare const bsonValue: BSONValue;
expectType<string>(bsonValue._bsontype);
expectType<(depth?: number | undefined, options?: unknown, inspect?: InspectFn | undefined) => string>(bsonValue.inspect);

expectNotDeprecated(new ObjectId('foo'));
expectDeprecated(new ObjectId(42));
expectNotDeprecated(new ObjectId(42 as string | number));