Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
ts-benchmark committed Aug 6, 2022
1 parent d7456e9 commit 611e66e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
24 changes: 22 additions & 2 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,28 @@ function gh12122() {

const Test3 = new Schema({ test: String }, { timestamps: true });

expectType<ApplySchemaOptions<{ test?: string; }, { timestamps: true }>>({} as InferSchemaType<typeof Test3>);
expectType<{
_id: Types.ObjectId;
__v: string;
test?: string | undefined;
createdAt: Date;
updatedAt: Date;
}>({} as InferSchemaType<typeof Test3>);

const Test4 = new Schema({ test: String }, { timestamps: true, versionKey: 'version' });
expectType<ApplySchemaOptions<{ test?: string; }, { timestamps: true, versionKey: 'version' }>>({} as InferSchemaType<typeof Test4>);
expectType<{
_id: Types.ObjectId;
version: string;
test?: string | undefined;
createdAt: Date;
updatedAt: Date;
}>({} as InferSchemaType<typeof Test4>);

const Test5 = new Schema({ _id: String, name: String });

expectType<{
_id: string;
version: string;
name?: string | undefined;
}>({} as InferSchemaType<typeof Test5>);
}
8 changes: 4 additions & 4 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ declare module 'mongoose' {
type ResolveSchemaOptions<T> = Omit<MergeType<DefaultSchemaOptions, T>, 'statics' | 'methods' | 'query' | 'virtuals'>;

type ApplySchemaOptions<T, O = DefaultSchemaOptions, P extends 'paths' | 'virtuals' = 'paths'> = FlatRecord<(P extends 'paths'
? ResolveTimestamps<Resolve__v<T, O extends Record<any, any> ? O : {} >, O>
? ResolveTimestamps<Resolve__v<Resolve_id<T, O>, O extends Record<any, any> ? O : {} >, O>
: ResolveId<T, O>
)>;
}
Expand All @@ -80,9 +80,9 @@ type ResolveId<T, O> = O extends { id: false }
? T
: T extends { id: any } ? T : MergeType<T, { id: string }>;

type Resolve_id<T, O> = T extends { _id: any }
type Resolve_id<T, O> = (keyof T extends '_id' ? true : false) extends true
? T
: O extends { _id: false } ? T : MergeType<T, { _id: Types.ObjectId }>;
: O extends { _id: false } ? T : MergeType<T, { _id: keyof T /* Types.ObjectId */ }>;

type Resolve__v<T, O extends Record<any, any>> = O extends { versionKey: false }
? T
Expand Down Expand Up @@ -198,5 +198,5 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
PathValueType extends Record<string, any> ? IfEquals<PathValueType, Types.ObjectId, Types.ObjectId, ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }>> :
unknown;

0 comments on commit 611e66e

Please sign in to comment.