Skip to content

Commit 3ced3a7

Browse files
Refactor the previous PR.
1 parent 2cbe0a0 commit 3ced3a7

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

test/types/schema.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Schema, Document, SchemaDefinition, Model, Types, InferSchemaType, SchemaType, Query, HydratedDocument } from 'mongoose';
22
import { expectType, expectError, expectAssignable } from 'tsd';
3-
import { IsTAny } from '../../types/inferschematype';
43

54
enum Genre {
65
Action,
@@ -364,6 +363,7 @@ export function autoTypedSchema() {
364363
array2?: Schema.Types.Mixed[];
365364
array3?: Schema.Types.Mixed[];
366365
array4?: Schema.Types.Mixed[];
366+
array5?: Schema.Types.Mixed[];
367367
};
368368

369369
const TestSchema = new Schema({
@@ -396,10 +396,11 @@ export function autoTypedSchema() {
396396
customSchema: Int8,
397397
map1: { type: Map, of: String },
398398
map2: { type: Map, of: Number },
399-
array1: { type: [String] },
400-
array2: { type: Array },
401-
array3: { type: [Schema.Types.Mixed] },
402-
array4: { type: [{}] }
399+
array1: [String],
400+
array2: Array,
401+
array3: [Schema.Types.Mixed],
402+
array4: [{}],
403+
array5: []
403404
});
404405

405406
type InferredTestSchemaType = InferSchemaType<typeof TestSchema>;

types/inferschematype.d.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,18 @@ declare module 'mongoose' {
5151
* @param {T} T A generic type to be checked.
5252
* @returns true if {@link T} is Record OR false if {@link T} is of any type.
5353
*/
54-
type IsItRecordAndNotAny<T> = IsTAny<T> extends true ? false : T extends Record<any, any> ? true : false;
54+
type IsItRecordAndNotAny<T> = IfEquals<T, any, false, T extends Record<any, any> ? true : false>;
5555

56-
type IsTAny<T> = keyof any extends keyof T ? (unknown extends T ? true : false) : false;
56+
/**
57+
* @summary Checks if two types are identical.
58+
* @param {T} T The first type to be compared with {@link U}.
59+
* @param {U} U The seconde type to be compared with {@link T}.
60+
* @param {Y} Y A type to be returned if {@link T} & {@link U} are identical.
61+
* @param {N} N A type to be returned if {@link T} & {@link U} are not identical.
62+
*/
63+
type IfEquals<T, U, Y = true, N = false> =
64+
(<G>() => G extends T ? 1 : 0) extends
65+
(<G>() => G extends U ? 1 : 0) ? Y : N;
5766

5867
/**
5968
* @summary Required path base type.
@@ -130,8 +139,7 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
130139
* @returns Number, "Number" or "number" will be resolved to string type.
131140
*/
132141
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}> =
133-
IsTAny<PathValueType> extends true ? Schema.Types.Mixed:
134-
PathValueType extends (infer Item)[] ? ResolvePathType<Item>[] :
142+
PathValueType extends (infer Item)[] ? IfEquals<Item, never, Schema.Types.Mixed, ResolvePathType<Item>>[] :
135143
PathValueType extends StringConstructor | 'string' | 'String' | typeof Schema.Types.String ? PathEnumOrString<Options['enum']> :
136144
PathValueType extends NumberConstructor | 'number' | 'Number' | typeof Schema.Types.Number ? number :
137145
PathValueType extends DateConstructor | 'date' | 'Date' | typeof Schema.Types.Date ? Date :

0 commit comments

Comments
 (0)