-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
docArray.test.ts
57 lines (42 loc) · 1.49 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
import { Schema, model, Document, Types, LeanDocument } from 'mongoose';
import { expectError, expectType } from 'tsd';
const schema: Schema = new Schema({ tags: [new Schema({ name: String })] });
interface Subdoc extends Document {
name: string
}
interface ITest extends Document {
tags: Types.DocumentArray<Subdoc>
}
const Test = model<ITest>('Test', schema);
void async function main() {
const doc: ITest = await Test.findOne().orFail();
doc.tags = new Types.DocumentArray<Subdoc>([]);
doc.set('tags', []);
const record: Subdoc = doc.tags.create({ name: 'test' });
doc.tags.push(record);
doc.tags.push({ name: 'test' });
await doc.save();
const _doc: LeanDocument<ITest> = await Test.findOne().orFail().lean();
_doc.tags[0].name.substring(1);
expectError(_doc.tags.create({ name: 'fail' }));
}();
// https://github.com/Automattic/mongoose/issues/10293
async function gh10293() {
interface ITest {
name: string;
arrayOfArray: Types.Array<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
};
}