Skip to content

Commit 1a308de

Browse files
authored
Merge pull request #15332 from Automattic/revert-15331-vkarpov15/gh-15301
Revert "types: make `init` hooks types accurately reflect runtime behavior"
2 parents 3e5533e + a384ead commit 1a308de

File tree

4 files changed

+117
-63
lines changed

4 files changed

+117
-63
lines changed

test/types/middleware.preposttypes.test.ts

+116-30
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,47 @@ schema.pre('init', function() {
6767
expectType<HydratedDocument<IDocument>>(this);
6868
});
6969

70+
schema.post('init', function(res) {
71+
expectType<HydratedDocument<IDocument>>(this);
72+
expectNotType<Query<any, any>>(res);
73+
});
74+
75+
schema.pre('init', { document: true, query: false }, function() {
76+
expectType<HydratedDocument<IDocument>>(this);
77+
});
78+
79+
schema.post('init', { document: true, query: false }, function(res) {
80+
expectType<HydratedDocument<IDocument>>(this);
81+
expectNotType<Query<any, any>>(res);
82+
});
83+
84+
schema.pre('init', { document: true, query: true }, function() {
85+
expectType<HydratedDocument<IDocument>>(this);
86+
});
87+
88+
schema.post('init', { document: true, query: true }, function(res) {
89+
expectType<HydratedDocument<IDocument>>(this);
90+
expectNotType<Query<any, any>>(res);
91+
});
92+
93+
schema.pre('init', { document: false, query: true }, function() {
94+
expectType<never>(this);
95+
});
96+
97+
schema.post('init', { document: false, query: true }, function(res) {
98+
expectType<never>(this);
99+
expectNotType<Query<any, any>>(res);
100+
});
101+
102+
schema.pre('init', { document: false, query: false }, function() {
103+
expectType<never>(this);
104+
});
105+
106+
schema.post('init', { document: false, query: false }, function(res) {
107+
expectType<never>(this);
108+
expectNotType<Query<any, any>>(res);
109+
});
110+
70111
schema.pre('estimatedDocumentCount', function() {
71112
expectType<Query<any, any>>(this);
72113
});
@@ -652,6 +693,51 @@ schema.post('deleteOne', { document: false, query: false }, function(res) {
652693
expectNotType<Query<any, any>>(res);
653694
});
654695

696+
schema.pre(['save', 'init'], function() {
697+
expectType<HydratedDocument<IDocument>>(this);
698+
});
699+
700+
schema.post(['save', 'init'], function(res) {
701+
expectType<HydratedDocument<IDocument>>(this);
702+
expectNotType<Query<any, any>>(res);
703+
});
704+
705+
schema.pre(['save', 'init'], { document: true, query: false }, function() {
706+
expectType<HydratedDocument<IDocument>>(this);
707+
});
708+
709+
schema.post(['save', 'init'], { document: true, query: false }, function(res) {
710+
expectType<HydratedDocument<IDocument>>(this);
711+
expectNotType<Query<any, any>>(res);
712+
});
713+
714+
schema.pre(['save', 'init'], { document: true, query: true }, function() {
715+
expectType<HydratedDocument<IDocument>>(this);
716+
});
717+
718+
schema.post(['save', 'init'], { document: true, query: true }, function(res) {
719+
expectType<HydratedDocument<IDocument>>(this);
720+
expectNotType<Query<any, any>>(res);
721+
});
722+
723+
schema.pre(['save', 'init'], { document: false, query: true }, function() {
724+
expectType<never>(this);
725+
});
726+
727+
schema.post(['save', 'init'], { document: false, query: true }, function(res) {
728+
expectType<never>(this);
729+
expectNotType<Query<any, any>>(res);
730+
});
731+
732+
schema.pre(['save', 'init'], { document: false, query: false }, function() {
733+
expectType<never>(this);
734+
});
735+
736+
schema.post(['save', 'init'], { document: false, query: false }, function(res) {
737+
expectType<never>(this);
738+
expectNotType<Query<any, any>>(res);
739+
});
740+
655741
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany'], function() {
656742
expectType<Query<any, any>>(this);
657743
});
@@ -742,137 +828,137 @@ schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct
742828
expectNotType<Query<any, any>>(res);
743829
});
744830

745-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], function() {
831+
schema.pre(['save', 'init', 'updateOne', 'deleteOne', 'validate'], function() {
746832
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
747833
});
748834

749-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], function(res) {
835+
schema.post(['save', 'init', 'updateOne', 'deleteOne', 'validate'], function(res) {
750836
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
751837
expectNotType<Query<any, any>>(res);
752838
});
753839

754-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function() {
840+
schema.pre(['save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function() {
755841
expectType<Query<any, any>>(this);
756842
});
757843

758-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function(res) {
844+
schema.post(['save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function(res) {
759845
expectType<Query<any, any>>(this);
760846
expectNotType<Query<any, any>>(res);
761847
});
762848

763-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function() {
849+
schema.pre(['save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function() {
764850
expectType<HydratedDocument<IDocument>>(this);
765851
});
766852

767-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function(res) {
853+
schema.post(['save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function(res) {
768854
expectType<HydratedDocument<IDocument>>(this);
769855
expectNotType<Query<any, any>>(res);
770856
});
771857

772-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function() {
858+
schema.pre(['save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function() {
773859
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
774860
});
775861

776-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function(res) {
862+
schema.post(['save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function(res) {
777863
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
778864
expectNotType<Query<any, any>>(res);
779865
});
780866

781-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function() {
867+
schema.pre(['save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function() {
782868
expectType<never>(this);
783869
});
784870

785-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function(res) {
871+
schema.post(['save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function(res) {
786872
expectType<never>(this);
787873
expectNotType<Query<any, any>>(res);
788874
});
789875

790-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], function() {
876+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], function() {
791877
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
792878
});
793879

794-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], function(res) {
880+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], function(res) {
795881
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
796882
expectNotType<Query<any, any>>(res);
797883
});
798884

799-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function() {
885+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function() {
800886
expectType<Query<any, any>>(this);
801887
});
802888

803-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function(res) {
889+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function(res) {
804890
expectType<Query<any, any>>(this);
805891
expectNotType<Query<any, any>>(res);
806892
});
807893

808-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function() {
894+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function() {
809895
expectType<HydratedDocument<IDocument>>(this);
810896
});
811897

812-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function(res) {
898+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function(res) {
813899
expectType<HydratedDocument<IDocument>>(this);
814900
expectNotType<Query<any, any>>(res);
815901
});
816902

817-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function() {
903+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function() {
818904
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
819905
});
820906

821-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function(res) {
907+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function(res) {
822908
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
823909
expectNotType<Query<any, any>>(res);
824910
});
825911

826-
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function() {
912+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function() {
827913
expectType<never>(this);
828914
});
829915

830-
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function(res) {
916+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function(res) {
831917
expectType<never>(this);
832918
expectNotType<Query<any, any>>(res);
833919
});
834920

835-
schema.pre(['save', 'updateOne', 'deleteOne', 'validate'], function() {
921+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], function() {
836922
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
837923
});
838924

839-
schema.post(['save', 'updateOne', 'deleteOne', 'validate'], function(res) {
925+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], function(res) {
840926
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
841927
expectNotType<Query<any, any>>(res);
842928
});
843929

844-
schema.pre(['save', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function() {
930+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function() {
845931
expectType<Query<any, any>>(this);
846932
});
847933

848-
schema.post(['save', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function(res) {
934+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: false, query: true }, function(res) {
849935
expectType<Query<any, any>>(this);
850936
expectNotType<Query<any, any>>(res);
851937
});
852938

853-
schema.pre(['save', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function() {
939+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function() {
854940
expectType<HydratedDocument<IDocument>>(this);
855941
});
856942

857-
schema.post(['save', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function(res) {
943+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: true, query: false }, function(res) {
858944
expectType<HydratedDocument<IDocument>>(this);
859945
expectNotType<Query<any, any>>(res);
860946
});
861947

862-
schema.pre(['save', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function() {
948+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function() {
863949
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
864950
});
865951

866-
schema.post(['save', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function(res) {
952+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: true, query: true }, function(res) {
867953
expectType<Query<any, any>|HydratedDocument<IDocument>>(this);
868954
expectNotType<Query<any, any>>(res);
869955
});
870956

871-
schema.pre(['save', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function() {
957+
schema.pre(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function() {
872958
expectType<never>(this);
873959
});
874960

875-
schema.post(['save', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function(res) {
961+
schema.post(['estimatedDocumentCount', 'countDocuments', 'deleteMany', 'distinct', 'find', 'findOne', 'findOneAndDelete', 'findOneAndReplace', 'findOneAndUpdate', 'replaceOne', 'updateMany', 'save', 'init', 'updateOne', 'deleteOne', 'validate'], { document: false, query: false }, function(res) {
876962
expectType<never>(this);
877963
expectNotType<Query<any, any>>(res);
878964
});

test/types/schema.test.ts

-31
Original file line numberDiff line numberDiff line change
@@ -1747,37 +1747,6 @@ async function schemaDouble() {
17471747
expectType<Types.Double | null | undefined>(doc.balance);
17481748
}
17491749

1750-
function gh15301() {
1751-
interface IUser {
1752-
time: { hours: number, minutes: number }
1753-
}
1754-
const userSchema = new Schema<IUser>({
1755-
time: {
1756-
type: new Schema(
1757-
{
1758-
hours: { type: Number, required: true },
1759-
minutes: { type: Number, required: true }
1760-
},
1761-
{ _id: false }
1762-
),
1763-
required: true
1764-
}
1765-
});
1766-
1767-
const timeStringToObject = (time) => {
1768-
if (typeof time !== 'string') return time;
1769-
const [hours, minutes] = time.split(':');
1770-
return { hours: parseInt(hours), minutes: parseInt(minutes) };
1771-
};
1772-
1773-
userSchema.pre('init', function(rawDoc) {
1774-
expectType<IUser>(rawDoc);
1775-
if (typeof rawDoc.time === 'string') {
1776-
rawDoc.time = timeStringToObject(rawDoc.time);
1777-
}
1778-
});
1779-
}
1780-
17811750
function defaultReturnsUndefined() {
17821751
const schema = new Schema<{ arr: number[] }>({
17831752
arr: {

0 commit comments

Comments
 (0)