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

InferSchemaType: consider { required: boolean } required if it isn't explicitly false #12784

Merged
merged 2 commits into from
Dec 16, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,3 +969,12 @@ function gh12611() {
anotherField?: string;
}>({} as Props);
}

function gh12782() {
const schemaObj = { test: { type: String, required: true } };
const schema = new Schema(schemaObj);
type Props = InferSchemaType<typeof schema>;
expectType<{
test: string
}>({} as Props);
}
18 changes: 11 additions & 7 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,19 @@ type IsPathDefaultUndefined<PathType> = PathType extends { default: undefined }
type IsPathRequired<P, TypeKey extends string = DefaultTypeKey> =
P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[]
? true
: P extends (Record<TypeKey, ArrayConstructor | any[]>)
? IsPathDefaultUndefined<P> extends true
: P extends { required: boolean }
? P extends { required: false }
? false
: true
: P extends (Record<TypeKey, any>)
? P extends { default: any }
? IfEquals<P['default'], undefined, false, true>
: false
: false;
: P extends (Record<TypeKey, ArrayConstructor | any[]>)
? IsPathDefaultUndefined<P> extends true
? false
: true
: P extends (Record<TypeKey, any>)
? P extends { default: any }
? IfEquals<P['default'], undefined, false, true>
: false
: false;

/**
* @summary Path base type defined by using TypeKey
Expand Down