-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
🐛fix: improve inferred Schema Type #12007
Merged
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
bb2ef25
🏷️types: fix types
iammola 2e51745
🏷️types: improve inferred types for arrays
iammola 73706cb
✅test: add tests
iammola 6277c4f
🏷️types: correct friendId type
iammola 687d4db
🐛fix: handle deeper schema arrays
iammola e3679a8
🐛fix: corrrectly filter `default: undefined` aray
iammola 79c9ce4
🐛fix: resolves type of nested paths that isn't a schema
iammola 7d280a2
🚨lint: fix indentation
iammola 2f1d2d9
🐛fix: use correct type in test schema
iammola 5ce27ac
✅test: remove mixed schema type test
iammola d31aa08
🏷️types: allow `Mixed`, `{}` and `ObjectConstructor` resolve to `unkn…
iammola 4123249
🏷️types: use `unknown` as expected mixed schema type
iammola d12af93
🐛fix: reviewed changes
iammola 4037696
Restore schema.test.ts
iammola 3def8c3
Revert "Restore schema.test.ts"
iammola 27614de
🚨lint: fix warnings
iammola 1abdb34
Fix TS errors And refactor inferschematype.d.ts file
mohammad0-0ahmad 1c82c05
Coverage for array path implicit type.
mohammad0-0ahmad c64f97b
Merge pull request #1 from mohammad0-0ahmad-forks/_12007
iammola 814a0db
🐛 fix: make paths with default specified required
iammola dc5e52a
🐛 fix: limit allowed types to be required with default
iammola 8aa7f53
✅ test: add test for schema paths with default
iammola ca6f97b
🚨 lint: fix errors
iammola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types, SchemaDefinitionProperty } from 'mongoose'; | ||
import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType, Types, NumberSchemaDefinition, StringSchemaDefinition, BooleanSchemaDefinition, DateSchemaDefinition } from 'mongoose'; | ||
|
||
declare module 'mongoose' { | ||
/** | ||
|
@@ -75,7 +75,7 @@ type IsPathRequired<P, TypeKey extends TypeKeyBaseType> = | |
? P extends { default: undefined } | ||
? false | ||
: true | ||
: P extends PathWithTypePropertyBaseType<TypeKey> | ||
: P extends (Record<TypeKey, NumberSchemaDefinition | StringSchemaDefinition | BooleanSchemaDefinition | DateSchemaDefinition>) | ||
? P extends { default: ResolvePathType<P[TypeKey]> } | ||
? true | ||
: false | ||
|
@@ -156,11 +156,11 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends ( | |
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}> = | ||
PathValueType extends Schema ? InferSchemaType<PathValueType> : | ||
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any, ResolvePathType<Item>>[] : | ||
PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString<Options['enum']> : | ||
PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number : | ||
PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date : | ||
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> : | ||
PathValueType extends NumberSchemaDefinition ? number : | ||
PathValueType extends DateSchemaDefinition ? Date : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are just because I noticed they're defined in their respective SchemaDefinition types. |
||
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer : | ||
PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean : | ||
PathValueType extends BooleanSchemaDefinition ? boolean : | ||
PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Types.ObjectId : | ||
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 : | ||
PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> : | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed not all of typeKey's types should be allowed to be remove the optional flag on that path.
So I set it to these 4. Because I think they're the only ones that should be able to provide defaults.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if you don't want the types to be limited, I'll revert the whole commit. @mohammad0-0ahmad