[mongodb] Fix support for optional fields in $pull and $push (#43892)

This commit is contained in:
Valentin Agachi 2020-04-16 00:18:19 +01:00 committed by GitHub
parent 498f646452
commit 8987bbd357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 7 deletions

View File

@ -31,6 +31,7 @@
// Linus Unnebäck <https://github.com/LinusU>
// Richard Bateman <https://github.com/taxilian>
// Igor Strebezhev <https://github.com/xamgore>
// Valentin Agachi <https://github.com/avaly>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
@ -1226,9 +1227,9 @@ export interface Collection<TSchema extends { [key: string]: any } = DefaultSche
}
/** Update Query */
type KeysOfAType<TSchema, Type> = { [key in keyof TSchema]: TSchema[key] extends Type ? key : never }[keyof TSchema];
type KeysOfAType<TSchema, Type> = { [key in keyof TSchema]: NonNullable<TSchema[key]> extends Type ? key : never }[keyof TSchema];
type KeysOfOtherType<TSchema, Type> = {
[key in keyof TSchema]: TSchema[key] extends Type ? never : key;
[key in keyof TSchema]: NonNullable<TSchema[key]> extends Type ? never : key;
}[keyof TSchema];
type AcceptedFields<TSchema, FieldType, AssignableType> = {

View File

@ -43,6 +43,8 @@ async function run() {
const _b: Bag = b; // b is larger than bag and may contain extra properties
});
collection.findOne<Bag>({ color: 'white' }).then(b => {
b.cost;
if (b) {
b.cost; // $ExpectType number
}
});
}

View File

@ -165,13 +165,13 @@ async function run() {
indexTypeResult1.insertedId; // $ExpectType ObjectId
// should not remove types of existing fields
indexTypeResult1.ops[0].stringField; // $ExpectType string
indexTypeResult1.ops[0].numberField; // $ExpectType number
indexTypeResult1.ops[0].numberField; // $ExpectType number | undefined
// should assign "any" type to any other field
indexTypeResult1.ops[0].randomField; // $ExpectType any
// should do the same for insertMany
indexTypeResultMany1.ops[0]._id; // $ExpectType ObjectId
indexTypeResultMany1.insertedIds; // $ExpectType { [key: number]: ObjectId; }
indexTypeResultMany1.ops[0].numberField; // $ExpectType number
indexTypeResultMany1.ops[0].numberField; // $ExpectType number | undefined
indexTypeResultMany1.ops[0].stringField; // $ExpectType string
indexTypeResultMany1.ops[0].randomField; // $ExpectType any

View File

@ -112,6 +112,7 @@ async function run() {
buildUpdateQuery({ $pull: { fruitTags: 'a' } });
buildUpdateQuery({ $pull: { fruitTags: { $in: ['a'] } } });
buildUpdateQuery({ $pull: { maybeFruitTags: 'apple' } });
buildUpdateQuery({ $pull: { 'dot.notation': 1 } });
buildUpdateQuery({ $pull: { 'subInterfaceArray.$[]': { $in: ['a'] } } });
buildUpdateQuery({ $pull: { subInterfaceArray: { field1: 'a' } }});
@ -124,6 +125,7 @@ async function run() {
buildUpdateQuery({ $push: { fruitTags: { $each: ['a'], $sort: 1 } } });
buildUpdateQuery({ $push: { fruitTags: { $each: ['a'], $sort: -1 } } });
buildUpdateQuery({ $push: { fruitTags: { $each: ['a'], $sort: { 'sub.field': -1 } } } });
buildUpdateQuery({ $push: { maybeFruitTags: 'apple' } });
// buildUpdateQuery({ $push: { fruitTags: { $each: ['stringField'] } } });
// buildUpdateQuery({ $push: { 'dot.notation': 1 } });
// buildUpdateQuery({ $push: { 'subInterfaceArray.$[]': { $in: ['a'] } } });

View File

@ -9,5 +9,7 @@ async function run() {
collection.stats((err: MongoError, stats: CollStats) => {});
const stats = await collection.stats();
stats.wiredTiger.cache['bytes currently in the cache']; // $ExpectType number
if (stats.wiredTiger) {
stats.wiredTiger.cache['bytes currently in the cache']; // $ExpectType number
}
}

View File

@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [