Skip to content

Commit

Permalink
Merge pull request #14534 from Automattic/vkarpov15/gh-14367-2
Browse files Browse the repository at this point in the history
types(schema): correctly infer Array<Schema.Types.*>
  • Loading branch information
vkarpov15 authored Apr 29, 2024
2 parents 3072c41 + 10f2fa7 commit af2afc1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
18 changes: 18 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1424,3 +1424,21 @@ function gh14496() {
}
});
}

function gh14367() {
const UserSchema = new Schema({
counts: [Schema.Types.Number],
roles: [Schema.Types.String],
dates: [Schema.Types.Date],
flags: [Schema.Types.Boolean]
});

type IUser = InferSchemaType<typeof UserSchema>;

const x: IUser = {
counts: [12],
roles: ['test'],
dates: [new Date('2016-06-01')],
flags: [true]
};
}
22 changes: 17 additions & 5 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,27 @@ type IsSchemaTypeFromBuiltinClass<T> = T extends (typeof String)
? true
: T extends (typeof Schema.Types.Decimal128)
? true
: T extends Types.ObjectId
: T extends (typeof Schema.Types.String)
? true
: T extends Types.Decimal128
: T extends (typeof Schema.Types.Number)
? true
: T extends Buffer
: T extends (typeof Schema.Types.Date)
? true
: T extends (typeof Schema.Types.Mixed)
: T extends (typeof Schema.Types.Boolean)
? true
: IfEquals<T, Schema.Types.ObjectId, true, false>;
: T extends (typeof Schema.Types.Buffer)
? true
: T extends Types.ObjectId
? true
: T extends Types.Decimal128
? true
: T extends Buffer
? true
: T extends NativeDate
? true
: T extends (typeof Schema.Types.Mixed)
? true
: IfEquals<T, Schema.Types.ObjectId, true, false>;

/**
* @summary Resolve path type by returning the corresponding type.
Expand Down

0 comments on commit af2afc1

Please sign in to comment.