Skip to content

Commit

Permalink
Infer nested schema array to Types.DocumentArray instead of normal ar…
Browse files Browse the repository at this point in the history
…ray.
  • Loading branch information
mohammad0-0ahmad committed Jul 4, 2022
1 parent a2ff573 commit 9128dbd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions test/types/schema.test.ts
Expand Up @@ -540,7 +540,7 @@ export type AutoTypedSchemaType = {
favoritDrink?: 'Tea' | 'Coffee',
favoritColorMode: 'dark' | 'light'
friendID?: Types.ObjectId;
nestedArray: Array<{
nestedArray: Types.DocumentArray<{
date: Date;
messages?: number;
}>
Expand Down Expand Up @@ -661,7 +661,12 @@ function gh12030() {
]
});

// expectType<{ users: Types.DocumentArray<{ userName?: string, credit: number }> }>({} as InferSchemaType<typeof Schema3>);
expectType<{
users: Types.DocumentArray<{
credit: number;
username?: string;
}>;
}>({} as InferSchemaType<typeof Schema3>);


const Schema4 = new Schema({
Expand All @@ -674,7 +679,7 @@ function gh12030() {
data: { type: { role: Object }, default: {} }
});

expectType<{ data: { role?: string } }>({} as InferSchemaType<typeof Schema5>);
expectType<{ data: { role?: any } }>({} as InferSchemaType<typeof Schema5>);

const Schema6 = new Schema({
track: {
Expand Down
2 changes: 1 addition & 1 deletion types/inferschematype.d.ts
Expand Up @@ -171,7 +171,7 @@ type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends (
*/
type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends TypeKeyBaseType = DefaultTypeKey> =
PathValueType extends Schema ? InferSchemaType<PathValueType> :
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any, ResolvePathType<Item>>[] :
PathValueType extends (infer Item)[] ? IfEquals<Item, never, any[], Item extends Schema ? Types.DocumentArray<ResolvePathType<Item>> : ResolvePathType<Item>[]> :
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
PathValueType extends NumberSchemaDefinition ? number :
PathValueType extends DateSchemaDefinition ? Date :
Expand Down

0 comments on commit 9128dbd

Please sign in to comment.