Skip to content

Commit

Permalink
fix(index.d.ts): support setting type to an array of schemas when u…
Browse files Browse the repository at this point in the history
…sing SchemaDefinitionType

Fix #9962
  • Loading branch information
vkarpov15 committed Mar 2, 2021
1 parent 9896ee2 commit bf5a96f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
7 changes: 6 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1371,8 +1371,13 @@ declare module 'mongoose' {
currentTime?: () => (Date | number);
}

type Unpacked<T> = T extends (infer U)[] ? U : T;

interface SchemaTypeOptions<T> {
type?: T extends string | number | Function ? SchemaDefinitionWithBuiltInClass<T> : T;
type?:
T extends string | number | Function ? SchemaDefinitionWithBuiltInClass<T> :
T extends object[] ? T | Schema<Unpacked<T> & Document>[] :
T;

/** Defines a virtual with the given name that gets/sets this path. */
alias?: string;
Expand Down
22 changes: 21 additions & 1 deletion test/typescript/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@ enum Genre {
Comedy
}

const movieSchema: Schema = new Schema({
interface Actor {
name: string,
age: number
}
const actorSchema =
new Schema<Actor & Document, Model<Actor & Document>, Actor>({ name: { type: String }, age: { type: Number } });

interface Movie {
title?: string,
featuredIn?: string,
rating?: number,
genre?: string,
actionIntensity?: number,
actors?: Actor[]
}

const movieSchema = new Schema<Document<Movie>, Model<Document<Movie>>, Movie>({
title: String,
featuredIn: {
type: String,
Expand All @@ -32,6 +48,10 @@ const movieSchema: Schema = new Schema({
},
'Action intensity required for action genre'
]
},
actors: {
type: [actorSchema],
default: undefined
}
});

Expand Down

0 comments on commit bf5a96f

Please sign in to comment.