From baea9f5a8a12b411f6e96d2e1174329048826f8b Mon Sep 17 00:00:00 2001 From: Phillip Huang Date: Tue, 3 Jan 2023 15:26:00 -0800 Subject: [PATCH 1/3] Correctly infer string enums on const arrays --- types/inferschematype.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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. From 17ee131daf3b2327e02312d787ad92274e2fa40f Mon Sep 17 00:00:00 2001 From: Phillip Huang Date: Tue, 3 Jan 2023 15:31:31 -0800 Subject: [PATCH 2/3] add test --- test/types/schema.test.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 2281560bcab..14a6708c831 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']); +} From 6f243a90fbc114c54a700d8a3d58424cd86b10df Mon Sep 17 00:00:00 2001 From: Phillip Huang Date: Tue, 3 Jan 2023 15:36:06 -0800 Subject: [PATCH 3/3] lint --- test/types/schema.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/types/schema.test.ts b/test/types/schema.test.ts index 14a6708c831..e6dc39744ac 100644 --- a/test/types/schema.test.ts +++ b/test/types/schema.test.ts @@ -986,19 +986,19 @@ function gh12816() { function gh12869() { const dbExampleConst = new Schema( { - active: { type: String, enum: ["foo", "bar"] as const, required: true } + active: { type: String, enum: ['foo', 'bar'] as const, required: true } } ); type ExampleConst = InferSchemaType; - expectType<"foo" | "bar">({} as ExampleConst['active']); + expectType<'foo' | 'bar'>({} as ExampleConst['active']); const dbExample = new Schema( { - active: { type: String, enum: ["foo", "bar"], required: true } + active: { type: String, enum: ['foo', 'bar'], required: true } } ); type Example = InferSchemaType; - expectType<"foo" | "bar">({} as Example['active']); + expectType<'foo' | 'bar'>({} as Example['active']); }