Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: allow defining document array using [{ prop: String }] syntax #14095

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions test/types/docArray.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,19 @@ function gh13087() {
};
}>>(points);
}

async function gh13424() {
const subDoc = {
name: { type: String, required: true },
controls: { type: String, required: true }
};

const testSchema = new Schema({
question: { type: String, required: true },
subDocArray: { type: [subDoc], required: true }
});
const TestModel = model('Test', testSchema);

const doc = new TestModel();
expectType<Types.ObjectId | undefined>(doc.subDocArray[0]._id);
}
33 changes: 25 additions & 8 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ export function autoTypedSchema() {
array1: string[];
array2: any[];
array3: any[];
array4: any[];
// array4: any[];
hasezoey marked this conversation as resolved.
Show resolved Hide resolved
array5: any[];
array6: string[];
array7?: string[] | null;
Expand Down Expand Up @@ -455,7 +455,7 @@ export function autoTypedSchema() {
array1: [String],
array2: Array,
array3: [Schema.Types.Mixed],
array4: [{}],
// array4: [{}],
hasezoey marked this conversation as resolved.
Show resolved Hide resolved
array5: [],
array6: { type: [String] },
array7: { type: [String], default: undefined },
Expand Down Expand Up @@ -669,9 +669,9 @@ function gh12030() {
username: { type: String }
}
]>;
expectType<{
expectType<Types.DocumentArray<{
username?: string | null
}[]>({} as A);
}>>({} as A);

type B = ObtainDocumentType<{
users: [
Expand All @@ -681,15 +681,15 @@ function gh12030() {
]
}>;
expectType<{
users: {
users: Types.DocumentArray<{
username?: string | null
}[];
}>;
}>({} as B);

expectType<{
users: {
users: Types.DocumentArray<{
username?: string | null
}[];
}>;
}>({} as InferSchemaType<typeof Schema1>);

const Schema2 = new Schema({
Expand Down Expand Up @@ -1351,3 +1351,20 @@ function gh14028_statics() {
// Trigger type assertions inside statics
schema.statics.createWithFullName('John Doe');
}

function gh13424() {
const subDoc = {
name: { type: String, required: true },
controls: { type: String, required: true }
};

const testSchema = {
question: { type: String, required: true },
subDocArray: { type: [subDoc], required: true }
};

const TestModel = model('TestModel', new Schema(testSchema));

const doc = new TestModel({});
expectType<Types.ObjectId | undefined>(doc.subDocArray[0]._id);
}
43 changes: 38 additions & 5 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
DefaultTypeKey,
ObjectIdSchemaDefinition,
IfEquals,
DefaultSchemaOptions
DefaultSchemaOptions,
IsItRecordAndNotAny
} from 'mongoose';

declare module 'mongoose' {
Expand Down Expand Up @@ -176,6 +177,30 @@ TypeKey
*/
type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends ReadonlyArray<infer E> ? E : T extends { values: any } ? PathEnumOrString<T['values']> : string;

type IsSchemaTypeFromBuiltinClass<T> = T extends (typeof String)
? true
: T extends (typeof Number)
? true
: T extends (typeof Boolean)
? true
: T extends (typeof Buffer)
? true
: T extends (typeof Schema.Types.ObjectId)
? true
: T extends (typeof Schema.Types.UUID)
? true
: T extends (typeof Schema.Types.Decimal128)
? true
: T extends Types.ObjectId
? true
: T extends Types.Decimal128
? true
: T extends Buffer
? true
: T extends (typeof Schema.Types.Mixed)
? true
: IfEquals<T, Schema.Types.ObjectId, true, false>;

/**
* @summary Resolve path type by returning the corresponding type.
* @param {PathValueType} PathValueType Document definition path type.
Expand All @@ -189,15 +214,19 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
IfEquals<Item, never, any[], Item extends Schema ?
// If Item is a schema, infer its type.
Types.DocumentArray<InferSchemaType<Item>> :
Item extends Record<TypeKey, any>?
Item extends Record<TypeKey, any> ?
Item[TypeKey] extends Function | String ?
// If Item has a type key that's a string or a callable, it must be a scalar,
// so we can directly obtain its path type.
ObtainDocumentPathType<Item, TypeKey>[] :
// If the type key isn't callable, then this is an array of objects, in which case
// we need to call ObtainDocumentType to correctly infer its type.
ObtainDocumentType<Item, any, { typeKey: TypeKey }>[]:
ObtainDocumentPathType<Item, TypeKey>[]
ObtainDocumentType<Item, any, { typeKey: TypeKey }>[] :
IsSchemaTypeFromBuiltinClass<Item> extends true ?
ObtainDocumentPathType<Item, TypeKey>[] :
IsItRecordAndNotAny<Item> extends true ?
Types.DocumentArray<ObtainDocumentType<Item, any, { typeKey: TypeKey }>> :
ObtainDocumentPathType<Item, TypeKey>[]
>:
PathValueType extends ReadonlyArray<infer Item> ?
IfEquals<Item, never, any[], Item extends Schema ?
Expand All @@ -206,7 +235,11 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
Item[TypeKey] extends Function | String ?
ObtainDocumentPathType<Item, TypeKey>[] :
ObtainDocumentType<Item, any, { typeKey: TypeKey }>[]:
ObtainDocumentPathType<Item, TypeKey>[]
IsSchemaTypeFromBuiltinClass<Item> extends true ?
ObtainDocumentPathType<Item, TypeKey>[] :
IsItRecordAndNotAny<Item> extends true ?
Types.DocumentArray<ObtainDocumentType<Item, any, { typeKey: TypeKey }>> :
ObtainDocumentPathType<Item, TypeKey>[]
>:
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
IfEquals<PathValueType, Schema.Types.String> extends true ? PathEnumOrString<Options['enum']> :
Expand Down