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

Fixed InferSchemaType nullable arrays type inference #12441

Closed
Closed
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
29 changes: 29 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,32 @@ function pluginOptions() {
schema.plugin<any, SomePluginOptions>(pluginFunction2, { option2: 0 });
expectError(schema.plugin<any, SomePluginOptions>(pluginFunction2, {})); // should error because "option2" is not optional
}

function gh12420() {
const TestSchema = new Schema(
{
comments: { type: [String], required: false },
reference: {
type: [
{
type: { type: String, required: false },
value: { type: String, required: false }
}
],
required: false
}
},
{
collection: 'tests',
timestamps: false
}
);

expectType<{
comments?: string[],
reference?: {
type?: string,
value?: string
}[]
}>({} as InferSchemaType<typeof TestSchema>);
}
2 changes: 1 addition & 1 deletion types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type IsPathRequired<P, TypeKey extends TypeKeyBaseType> =
P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[]
? true
: P extends (Record<TypeKey, ArrayConstructor | any[]>)
? P extends { default: undefined }
? P extends { default: undefined } | { required: false }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comments here: #12420 (comment) . We shouldn't rely on required: false, because required is false by default anyway. We instead need to check if default is a function that returns undefined.

? false
: true
: P extends (Record<TypeKey, any>)
Expand Down