-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
docArray.test.ts
164 lines (139 loc) · 4.15 KB
/
docArray.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Schema, model, Model, Types, InferSchemaType } from 'mongoose';
import { expectError, expectType } from 'tsd';
async function gh10293() {
interface ITest {
name: string;
arrayOfArray: string[][]; // <-- Array of Array
}
const testSchema = new Schema<ITest>({
name: {
type: String,
required: true
},
arrayOfArray: [[String]]
});
const TestModel = model('gh10293TestModel', testSchema);
testSchema.methods.getArrayOfArray = function(this: InstanceType<typeof TestModel>): string[][] { // <-- function to return Array of Array
const test = this.toObject();
expectType<string[][]>(test.arrayOfArray);
return test.arrayOfArray; // <-- error here if the issue persisted
};
}
function gh13087() {
interface Book {
author: {
name: string;
};
}
expectError(new Types.DocumentArray<Book>([1, 2, 3]));
const locationSchema = new Schema(
{
type: {
required: true,
type: String,
enum: ['Point']
},
coordinates: {
required: true,
type: [Number] // [longitude, latitude]
}
},
{ _id: false }
);
const pointSchema = new Schema({
name: { required: true, type: String },
location: { required: true, type: locationSchema }
});
const routeSchema = new Schema({
points: { type: [pointSchema] }
});
type Route = InferSchemaType<typeof routeSchema>;
function getTestRouteData(): Route {
return {
points: new Types.DocumentArray([
{ name: 'Test', location: { type: 'Point', coordinates: [1, 2] } }
])
};
}
const { points } = getTestRouteData();
expectType<Types.DocumentArray<{
name: string;
location: {
type: 'Point';
coordinates: number[];
};
}>>(points);
}
async function gh13424() {
const subDoc = {
name: { type: String, required: true },
controls: { type: String, required: true }
};
const testSchema = new Schema({
question: { type: String, required: true },
subDocArray: { type: [subDoc], required: true }
});
const TestModel = model('Test', testSchema);
const doc = new TestModel();
expectType<Types.ObjectId | undefined>(doc.subDocArray[0]._id);
}
async function gh14367() {
const UserSchema = new Schema(
{
reminders: {
type: [
{
type: { type: Schema.Types.String },
date: { type: Schema.Types.Date },
toggle: { type: Schema.Types.Boolean },
notified: { type: Schema.Types.Boolean }
}
],
default: [
{ type: 'vote', date: new Date(), toggle: false, notified: false },
{ type: 'daily', date: new Date(), toggle: false, notified: false },
{ type: 'drop', date: new Date(), toggle: false, notified: false },
{ type: 'claim', date: new Date(), toggle: false, notified: false },
{ type: 'work', date: new Date(), toggle: false, notified: false }
]
},
avatar: {
type: Schema.Types.String
}
},
{ timestamps: true }
);
type IUser = InferSchemaType<typeof UserSchema>;
expectType<string | null | undefined>({} as IUser['reminders'][0]['type']);
expectType<Date | null | undefined>({} as IUser['reminders'][0]['date']);
expectType<boolean | null | undefined>({} as IUser['reminders'][0]['toggle']);
expectType<string | null | undefined>({} as IUser['avatar']);
}
function gh14469() {
interface Names {
_id: Types.ObjectId;
firstName: string;
}
// Document definition
interface User {
names: Names[];
}
// TMethodsAndOverrides
type UserDocumentProps = {
names: Types.DocumentArray<Names>;
};
type UserModelType = Model<User, {}, UserDocumentProps>;
const userSchema = new Schema<User, UserModelType>(
{
names: [new Schema<Names>({ firstName: String })]
},
{ timestamps: true }
);
// Create model
const UserModel = model<User, UserModelType>('User', userSchema);
const doc = new UserModel({ names: [{ firstName: 'John' }] });
const jsonDoc = doc?.toJSON();
expectType<string>(jsonDoc?.names[0]?.firstName);
const jsonNames = doc?.names[0]?.toJSON();
expectType<string>(jsonNames?.firstName);
}