forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_helpers.test-d.ts
54 lines (44 loc) · 2.24 KB
/
schema_helpers.test-d.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
import type {
InferIdType,
WithId,
WithoutId,
OptionalId,
EnhancedOmit
} from '../../src/mongo_types';
import { expectType, expectNotType, expectAssignable } from 'tsd';
import { Document, ObjectId } from 'bson';
// InferIdType
expectType<InferIdType<Document>>(new ObjectId());
expectType<InferIdType<{ _id: number }>>(1 + 1);
expectType<InferIdType<{ a: number } | { b: string }>>(new ObjectId());
expectAssignable<InferIdType<{ _id: number } | { b: string }>>(new ObjectId());
expectAssignable<InferIdType<{ _id: number } | { b: string }>>(1 + 1);
// WithId
expectAssignable<WithId<Document>>({ _id: new ObjectId() });
expectAssignable<WithId<{ a: number }>>({ _id: new ObjectId(), a: 3 });
expectAssignable<WithId<{ _id: ObjectId }>>({ _id: new ObjectId() });
expectAssignable<WithId<{ _id: number }>>({ _id: 5 });
expectNotType<WithId<Document>>({ _id: 3 });
// Changing _id to a type other than ObjectId makes it required:
expectNotType<OptionalId<{ _id: number; a: number }>>({ a: 3 });
expectNotType<OptionalId<{ _id: number; a: number } | { _id: ObjectId; b: number }>>({ a: 3 });
// If you just have an indexed type there is no enforcement
// expectType<OptionalId<{ [x: string]: number }>>({ a: 3 });
// But you can still bring back enforcement yourself
expectNotType<OptionalId<{ _id: number; [x: string]: number }>>({ a: 3 });
// Custom _id type
class MyId {}
expectNotType<OptionalId<{ _id: MyId; a: number }>>({ a: 3 });
expectNotType<OptionalId<{ _id: MyId; a: number }>>({ _id: new ObjectId(), a: 3 });
// WithoutId removes _id whether defined in the schema or not
expectType<WithoutId<{ _id: number; a: number }>>({ a: 2 });
expectNotType<WithoutId<{ _id: number; a: number }>>({ _id: 3, a: 2 });
expectNotType<WithoutId<{ a: number }>>({ _id: 3, a: 2 });
// EnhancedOmit fixes a problem with Typescript's built in Omit that breaks discriminated unions
// NODE-3287
// expectNotAssignable<EnhancedOmit<{ a: 'one' } | { b: 'two' }, 'a'>>({
// a: 'one' as const
// });
expectAssignable<Omit<{ a: 'one' } | { b: 'two' }, 'type'>>({ type: 'one' }); // This shouldn't work, but does, hence needing EnhancedOmit
// Indexed type is unaffected, because you can't omit from {[x: string]: any}
expectAssignable<EnhancedOmit<Document, 'omitted'>>({ omitted: 2 });