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
2 changes: 1 addition & 1 deletion scripts/tsc-diagnostics-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs');

const stdin = fs.readFileSync(0).toString('utf8');
const maxInstantiations = isNaN(process.argv[2]) ? 300000 : parseInt(process.argv[2], 10);
const maxInstantiations = isNaN(process.argv[2]) ? 350000 : parseInt(process.argv[2], 10);

console.log(stdin);

Expand Down
17 changes: 17 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1999,3 +1999,20 @@ function autoInferredNestedMaps() {
const doc = new TestModel({ nestedMap: new Map([['1', new Map([['2', 'value']])]]) });
expectType<Map<string, Map<string, string>>>(doc.nestedMap);
}

function gh15878() {
const schema = new Schema({
name: {
type: String,
default: null
},
age: {
type: Number,
default: () => null
}
});
const TestModel = model('Test', schema);
const doc = new TestModel({ name: 'John', age: 30 });
expectType<string | null | undefined>(doc.name);
expectType<number | null | undefined>(doc.age);
}
2 changes: 1 addition & 1 deletion types/inferrawdoctype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ declare module 'mongoose' {
ResolveRawPathType<Options['of'] extends ReadonlyArray<infer Item> ? Item : never>
: PathValueType extends ArrayConstructor ? any[]
: PathValueType extends typeof Schema.Types.Mixed ? any
: IfEquals<PathValueType, ObjectConstructor> extends true ? any
: PathValueType extends ObjectConstructor ? any
: IfEquals<PathValueType, {}> extends true ? any
: PathValueType extends typeof SchemaType ? PathValueType['prototype']
: PathValueType extends Record<string, any> ? InferRawDocType<PathValueType>
Expand Down
39 changes: 16 additions & 23 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ declare module 'mongoose' {
: T;
}

type IsPathDefaultUndefined<PathType> =
PathType extends { default: undefined } ? true
: PathType extends { default: (...args: any[]) => undefined } ? true
: false;

type RequiredPropertyDefinition =
| {
required: true | string | [true, string | undefined] | { isRequired: true };
Expand All @@ -150,16 +145,17 @@ type IsPathRequired<P, TypeKey extends string = DefaultTypeKey> =
P extends { required: false } ?
false
: true
: P extends Record<TypeKey, ArrayConstructor | any[]> ?
IsPathDefaultUndefined<P> extends true ?
false
: true
: P extends Record<TypeKey, any> ?
P extends { default: any } ?
IfEquals<P['default'], undefined, false, true>
: false
: P extends { default: undefined | null | ((...args: any[]) => undefined) | ((...args: any[]) => null) } ? false
: P extends { default: any } ? true
: P extends Record<TypeKey, ArrayConstructor | any[]> ? true
: false;

// Internal type used to efficiently check for never or any types
// can be efficiently checked like:
// `[T] extends [neverOrAny] ? T : ...`
// to avoid edge cases
type neverOrAny = ' ~neverOrAny~';

/**
* @summary A Utility to obtain schema's required path keys.
* @param {T} T A generic refers to document definition.
Expand Down Expand Up @@ -238,6 +234,7 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> =

type IsSchemaTypeFromBuiltinClass<T> =
T extends typeof String ? true
: unknown extends Buffer ? false
: T extends typeof Number ? true
: T extends typeof Boolean ? true
: T extends typeof Buffer ? true
Expand All @@ -254,7 +251,6 @@ type IsSchemaTypeFromBuiltinClass<T> =
: T extends Types.Decimal128 ? true
: T extends NativeDate ? true
: T extends typeof Schema.Types.Mixed ? true
: unknown extends Buffer ? false
: T extends Buffer ? true
: false;

Expand All @@ -270,12 +266,10 @@ type ResolvePathType<
Options extends SchemaTypeOptions<PathValueType> = {},
TypeKey extends string = DefaultSchemaOptions['typeKey'],
TypeHint = never
> = IfEquals<
TypeHint,
never,
PathValueType extends Schema ? InferSchemaType<PathValueType>
> = [TypeHint] extends [never]
? PathValueType extends Schema ? InferSchemaType<PathValueType>
: PathValueType extends AnyArray<infer Item> ?
IfEquals<Item, never> extends true
[Item] extends [never]
? any[]
: Item extends Schema ?
// If Item is a schema, infer its type.
Expand Down Expand Up @@ -314,7 +308,7 @@ type ResolvePathType<
: never
: PathValueType extends ArrayConstructor ? any[]
: PathValueType extends typeof Schema.Types.Mixed ? any
: IfEquals<PathValueType, ObjectConstructor> extends true ? any
: PathValueType extends ObjectConstructor ? any
: IfEquals<PathValueType, {}> extends true ? any
: PathValueType extends typeof SchemaType ? PathValueType['prototype']
: PathValueType extends Record<string, any> ?
Expand All @@ -325,6 +319,5 @@ type ResolvePathType<
typeKey: TypeKey;
}
>
: unknown,
TypeHint
>;
: unknown
: TypeHint;
6 changes: 3 additions & 3 deletions types/virtuals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare module 'mongoose' {
type TVirtualPathFN<DocType = {}, PathType = unknown, TInstanceMethods = {}, TReturn = unknown> =
<T = HydratedDocument<DocType, TInstanceMethods>>(this: Document<any, any, DocType> & DocType, value: PathType, virtual: VirtualType<T>, doc: Document<any, any, DocType> & DocType) => TReturn;

type SchemaOptionsVirtualsPropertyType<DocType = any, VirtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
[K in keyof VirtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, VirtualPaths[K], TInstanceMethods>
};
type SchemaOptionsVirtualsPropertyType<DocType = any, VirtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
[K in keyof VirtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, VirtualPaths[K], TInstanceMethods>
};
}