Skip to content

Commit

Permalink
fix(types): infer schematype type from schema paths when calling `Sch…
Browse files Browse the repository at this point in the history
…emaType.path()`

Fix #11987
  • Loading branch information
vkarpov15 committed Jul 2, 2022
1 parent af5f776 commit 8eb0eb4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/types/schema.test.ts
Expand Up @@ -612,3 +612,21 @@ function gh12003() {

expectType<{ name?: string }>({} as BaseSchemaType);
}

function gh11987() {
interface IUser {
name: string;
email: string;
organization: Types.ObjectId;
}

const userSchema = new Schema<IUser>({
name: { type: String, required: true },
email: { type: String, required: true },
organization: { type: Schema.Types.ObjectId, ref: 'Organization' }
});

expectType<SchemaType<string>>(userSchema.path<'name'>('name'));
expectError(userSchema.path<'foo'>('name'));
expectType<SchemaTypeOptions<string>>(userSchema.path<'name'>('name').OptionsConstructor);
}
1 change: 1 addition & 0 deletions types/index.d.ts
Expand Up @@ -227,6 +227,7 @@ declare module 'mongoose' {
obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>>;

/** Gets/sets schema paths. */
path<pathGeneric extends keyof EnforcedDocType>(path: pathGeneric): SchemaType<EnforcedDocType[pathGeneric]>;
path<ResultType extends SchemaType = SchemaType>(path: string): ResultType;
path(path: string, constructor: any): this;

Expand Down

0 comments on commit 8eb0eb4

Please sign in to comment.