Skip to content

Commit

Permalink
Merge pull request #15274 from Automattic/vkarpov15/add-double
Browse files Browse the repository at this point in the history
types: add mongoose.Types.Double
  • Loading branch information
vkarpov15 authored Feb 25, 2025
2 parents 3af7b50 + 6be3df0 commit 00ca5c8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
13 changes: 13 additions & 0 deletions lib/types/double.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Double type constructor
*
* #### Example:
*
* const pi = new mongoose.Types.Double(3.1415);
*
* @constructor Double
*/

'use strict';

module.exports = require('bson').Double;
1 change: 1 addition & 0 deletions lib/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ exports.Document = // @deprecate
exports.Embedded = require('./arraySubdocument');

exports.DocumentArray = require('./documentArray');
exports.Double = require('./double');
exports.Decimal128 = require('./decimal128');
exports.ObjectId = require('./objectid');

Expand Down
8 changes: 8 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1738,3 +1738,11 @@ function gh15244() {
const schema = new Schema({});
schema.discriminator('Name', new Schema({}), { value: 'value' });
}

async function schemaDouble() {
const schema = new Schema({ balance: 'Double' });
const TestModel = model('Test', schema);

const doc = await TestModel.findOne().orFail();
expectType<Types.Double | null | undefined>(doc.balance);
}
21 changes: 11 additions & 10 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,15 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
IfEquals<PathValueType, BigInt> extends true ? bigint :
PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | typeof BigInt ? bigint :
PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
PathValueType extends MapConstructor | 'Map' ? Map<string, ResolvePathType<Options['of']>> :
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
unknown,
PathValueType extends 'double' | 'Double' | typeof Schema.Types.Double ? Types.Double :
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
PathValueType extends MapConstructor | 'Map' ? Map<string, ResolvePathType<Options['of']>> :
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
unknown,
TypeHint>;
2 changes: 2 additions & 0 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,7 @@ declare module 'mongoose' {
}

class UUID extends bson.UUID {}

class Double extends bson.Double {}
}
}

0 comments on commit 00ca5c8

Please sign in to comment.