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

🐛fix: improve inferred Schema Type #12007

Merged
merged 23 commits into from Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bb2ef25
🏷️types: fix types
iammola Jun 28, 2022
2e51745
🏷️types: improve inferred types for arrays
iammola Jun 28, 2022
73706cb
✅test: add tests
iammola Jun 28, 2022
6277c4f
🏷️types: correct friendId type
iammola Jun 28, 2022
687d4db
🐛fix: handle deeper schema arrays
iammola Jun 28, 2022
e3679a8
🐛fix: corrrectly filter `default: undefined` aray
iammola Jun 28, 2022
79c9ce4
🐛fix: resolves type of nested paths that isn't a schema
iammola Jun 29, 2022
7d280a2
🚨lint: fix indentation
iammola Jun 29, 2022
2f1d2d9
🐛fix: use correct type in test schema
iammola Jun 29, 2022
5ce27ac
✅test: remove mixed schema type test
iammola Jun 29, 2022
d31aa08
🏷️types: allow `Mixed`, `{}` and `ObjectConstructor` resolve to `unkn…
iammola Jun 29, 2022
4123249
🏷️types: use `unknown` as expected mixed schema type
iammola Jun 29, 2022
d12af93
🐛fix: reviewed changes
iammola Jun 30, 2022
4037696
Restore schema.test.ts
iammola Jun 30, 2022
3def8c3
Revert "Restore schema.test.ts"
iammola Jun 30, 2022
27614de
🚨lint: fix warnings
iammola Jun 30, 2022
1abdb34
Fix TS errors And refactor inferschematype.d.ts file
mohammad0-0ahmad Jun 30, 2022
1c82c05
Coverage for array path implicit type.
mohammad0-0ahmad Jun 30, 2022
c64f97b
Merge pull request #1 from mohammad0-0ahmad-forks/_12007
iammola Jun 30, 2022
814a0db
🐛 fix: make paths with default specified required
iammola Jul 1, 2022
dc5e52a
🐛 fix: limit allowed types to be required with default
iammola Jul 1, 2022
8aa7f53
✅ test: add test for schema paths with default
iammola Jul 1, 2022
ca6f97b
🚨 lint: fix errors
iammola Jul 1, 2022
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
34 changes: 25 additions & 9 deletions test/types/schema.test.ts
Expand Up @@ -374,12 +374,12 @@ export function autoTypedSchema() {
boolean2?: boolean;
boolean3?: boolean;
boolean4?: boolean;
mixed1?: any;
mixed2?: any;
mixed3?: any;
objectId1?: Schema.Types.ObjectId;
objectId2?: Schema.Types.ObjectId;
objectId3?: Schema.Types.ObjectId;
mixed1?: unknown;
iammola marked this conversation as resolved.
Show resolved Hide resolved
mixed2?: unknown;
mixed3?: unknown;
objectId1?: Types.ObjectId;
iammola marked this conversation as resolved.
Show resolved Hide resolved
objectId2?: Types.ObjectId;
iammola marked this conversation as resolved.
Show resolved Hide resolved
objectId3?: Types.ObjectId;
customSchema?: Int8;
map1?: Map<string, string>;
map2?: Map<string, number>;
Expand All @@ -388,9 +388,9 @@ export function autoTypedSchema() {
array3?: any[];
array4?: any[];
array5?: any[];
decimal1?: Schema.Types.Decimal128;
decimal2?: Schema.Types.Decimal128;
decimal3?: Schema.Types.Decimal128;
decimal1?: Types.Decimal128;
iammola marked this conversation as resolved.
Show resolved Hide resolved
decimal2?: Types.Decimal128;
decimal3?: Types.Decimal128;
};

const TestSchema = new Schema({
Expand Down Expand Up @@ -475,6 +475,17 @@ export function autoTypedSchema() {
message: '{VALUE} is not supported'
},
required: true
},
friendID: {
type: Schema.Types.ObjectId
},
nestedArray: {
type: [
new Schema({
date: { type: Date, required: true },
messages: Number
})
]
}
}, {
statics: {
Expand Down Expand Up @@ -516,6 +527,11 @@ export type AutoTypedSchemaType = {
},
favoritDrink?: 'Tea' | 'Coffee',
favoritColorMode: 'dark' | 'light'
friendID?: Types.ObjectId;
nestedArray: Array<{
iammola marked this conversation as resolved.
Show resolved Hide resolved
date: Date;
messages?: number;
}>
}
, statics: {
staticFn: () => 'Returned from staticFn'
Expand Down
75 changes: 42 additions & 33 deletions types/inferschematype.d.ts
@@ -1,4 +1,4 @@
import { Schema, InferSchemaType, SchemaType, SchemaTypeOptions, TypeKeyBaseType } from 'mongoose';
iammola marked this conversation as resolved.
Show resolved Hide resolved
import { Schema, Types, InferSchemaType, ObtainDocumentType, SchemaDefinitionProperty, SchemaType, SchemaTypeOptions, TypeKeyBaseType } from 'mongoose';

declare module 'mongoose' {
/**
Expand All @@ -10,8 +10,8 @@ declare module 'mongoose' {
*/
type ObtainDocumentType<DocDefinition, EnforcedDocType = any, TypeKey extends TypeKeyBaseType = DefaultTypeKey> =
IsItRecordAndNotAny<EnforcedDocType> extends true ? EnforcedDocType : {
[K in keyof (RequiredPaths<DocDefinition> &
OptionalPaths<DocDefinition>)]: ObtainDocumentPathType<DocDefinition[K], TypeKey>;
[K in keyof (RequiredPaths<DocDefinition, TypeKey> &
OptionalPaths<DocDefinition, TypeKey>)]: ObtainDocumentPathType<DocDefinition[K], TypeKey>;
};

/**
Expand All @@ -23,7 +23,7 @@ declare module 'mongoose' {
* // result
* type UserType = {userName?: string}
*/
type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'> ;
type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'>;

/**
* @summary Obtains schema Generic type by using generic alias.
Expand Down Expand Up @@ -74,42 +74,50 @@ type RequiredPathBaseType = { required: true | [true, string | undefined] };
* @description It helps to check if a path is defined by TypeKey OR not.
* @param {TypeKey} TypeKey A literal string refers to path type property key.
*/
type PathWithTypePropertyBaseType<TypeKey extends TypeKeyBaseType> = { [k in TypeKey]: any };
type PathWithTypePropertyBaseType<TypeKey extends TypeKeyBaseType, T = any> = {
[k in TypeKey]: T;
};

/**
* @summary A Utility to obtain schema's required path keys.
* @param {T} T A generic refers to document definition.
* @returns required paths keys of document definition.
*/
type RequiredPathKeys<T> = {
[K in keyof T]: T[K] extends RequiredPathBaseType ? IfEquals<T[K], any, never, K> : never;
type RequiredPathKeys<T, TypeKey extends TypeKeyBaseType> = {
[K in keyof T]: T[K] extends RequiredPathBaseType ? IfEquals<T[K], any, never, K>
: T[K] extends PathWithTypePropertyBaseType<TypeKey, any[]>
? T[K] extends { default: undefined } ? never : K
: never;
}[keyof T];

/**
* @summary A Utility to obtain schema's required paths.
* @param {T} T A generic refers to document definition.
* @returns a record contains required paths with the corresponding type.
*/
type RequiredPaths<T> = {
[K in RequiredPathKeys<T>]: T[K];
type RequiredPaths<T, TypeKey extends TypeKeyBaseType> = {
[K in RequiredPathKeys<T, TypeKey>]: T[K];
};

/**
* @summary A Utility to obtain schema's optional path keys.
* @param {T} T A generic refers to document definition.
* @returns optional paths keys of document definition.
*/
type OptionalPathKeys<T> = {
[K in keyof T]: T[K] extends RequiredPathBaseType ? never : K;
type OptionalPathKeys<T, TypeKey extends TypeKeyBaseType> = {
[K in keyof T]: T[K] extends RequiredPathBaseType ? never :
T[K] extends PathWithTypePropertyBaseType<TypeKey, any[]>
? T[K] extends { default: undefined } ? K : never
: K;
}[keyof T];

/**
* @summary A Utility to obtain schema's optional paths.
* @param {T} T A generic refers to document definition.
* @returns a record contains optional paths with the corresponding type.
*/
type OptionalPaths<T> = {
[K in OptionalPathKeys<T>]?: T[K];
type OptionalPaths<T, TypeKey extends TypeKeyBaseType> = {
[K in OptionalPathKeys<T, TypeKey>]?: T[K];
};

/**
Expand All @@ -120,10 +128,13 @@ type OptionalPaths<T> = {
*/
type ObtainDocumentPathType<PathValueType, TypeKey extends TypeKeyBaseType> = PathValueType extends Schema<any>
? InferSchemaType<PathValueType>
: ResolvePathType<
PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? PathValueType[TypeKey] : PathValueType,
PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? Omit<PathValueType, TypeKey> : {}
>;
: PathValueType extends PathWithTypePropertyBaseType<TypeKey, Schema<any>>
? InferSchemaType<PathValueType[TypeKey]>
: ResolvePathType<
TypeKey,
PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? PathValueType[TypeKey] : PathValueType,
PathValueType extends PathWithTypePropertyBaseType<TypeKey> ? Omit<PathValueType, TypeKey> : {}
>;

/**
* @param {T} T A generic refers to string path enums.
Expand All @@ -137,19 +148,17 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
* @param {Options} Options Document definition path options except path type.
* @returns Number, "Number" or "number" will be resolved to string type.
*/
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<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 typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? boolean :
PathValueType extends 'objectId' | 'ObjectId' | typeof Schema.Types.ObjectId ? Schema.Types.ObjectId :
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Schema.Types.Decimal128 :
PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
unknown;
type ResolvePathType<TypeKey extends TypeKeyBaseType, PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}> =
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any, ObtainDocumentPathType<Item, TypeKey>>[] :
PathValueType extends { [K: string]: SchemaDefinitionProperty } ? ObtainDocumentType<PathValueType, any, TypeKey> :
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 typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
PathValueType extends BooleanConstructor | 'boolean' | 'Boolean' | typeof Schema.Types.Boolean ? 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<TypeKey, Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
unknown;