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

Correctly infer string enums on const arrays #12870

Merged
merged 3 commits into from Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions test/types/schema.test.ts
Expand Up @@ -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<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 @@ -162,7 +162,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