diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 2281560bcab..e6dc39744ac 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -982,3 +982,23 @@ function gh12782() { function gh12816() { const schema = new Schema({}, { overwriteModels: true }); } + +function gh12869() { + const dbExampleConst = new Schema( + { + active: { type: String, enum: ['foo', 'bar'] as const, required: true } + } + ); + + type ExampleConst = InferSchemaType; + expectType<'foo' | 'bar'>({} as ExampleConst['active']); + + const dbExample = new Schema( + { + active: { type: String, enum: ['foo', 'bar'], required: true } + } + ); + + type Example = InferSchemaType; + expectType<'foo' | 'bar'>({} as Example['active']); +} diff --git a/types/inferschematype.d.ts b/types/inferschematype.d.ts index 1b24d6daa32..b0e2dc73e2e 100644 --- a/types/inferschematype.d.ts +++ b/types/inferschematype.d.ts @@ -162,7 +162,7 @@ type ObtainDocumentPathType['enum']> = T extends (infer E)[] ? E : T extends { values: any } ? PathEnumOrString : string; +type PathEnumOrString['enum']> = T extends ReadonlyArray ? E : T extends { values: any } ? PathEnumOrString : string; /** * @summary Resolve path type by returning the corresponding type.