Skip to content

Commit

Permalink
Merge pull request #12870 from JavaScriptBach/const-array
Browse files Browse the repository at this point in the history
Correctly infer string enums on const arrays
  • Loading branch information
vkarpov15 committed Jan 6, 2023
2 parents a2af383 + 6f243a9 commit fe9bc23
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions test/types/schema.test.ts
Expand Up @@ -998,3 +998,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<typeof dbExampleConst>;
expectType<'foo' | 'bar'>({} as ExampleConst['active']);

const dbExample = new Schema(
{
active: { type: String, enum: ['foo', 'bar'], required: true }
}
);

type Example = InferSchemaType<typeof dbExample>;
expectType<'foo' | 'bar'>({} as Example['active']);
}
2 changes: 1 addition & 1 deletion types/inferschematype.d.ts
Expand Up @@ -168,7 +168,7 @@ type ObtainDocumentPathType<PathValueType, TypeKey extends string = DefaultTypeK
* @param {T} T A generic refers to string path enums.
* @returns Path enum values type as literal strings or string.
*/
type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (infer E)[] ? E : T extends { values: any } ? PathEnumOrString<T['values']> : string;
type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends ReadonlyArray<infer E> ? E : T extends { values: any } ? PathEnumOrString<T['values']> : string;

/**
* @summary Resolve path type by returning the corresponding type.
Expand Down

0 comments on commit fe9bc23

Please sign in to comment.