-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(NODE-3852,NODE-3854,NODE-3856): Misc typescript fixes for 4.3.1 #3102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3eb6ccc
Extend Filter type to support querying on a document
baileympearson 89a4fc9
Fix the findOneAnd<operation> return types to include _id
baileympearson d3b21bb
Add circular type fix
baileympearson bc09800
Export helper type
baileympearson 19ef058
Add rough support for self-referential types
baileympearson 879d909
staging temporarily
baileympearson dbb0063
Add quick comment
baileympearson 18a9232
Stage for swap branches
baileympearson 85e4261
sort through tests
baileympearson 178018b
Recursive union types working
baileympearson 4fdb78f
Add support for recursive union array types
baileympearson 85ffbfd
Break recursive type tests into separate file
baileympearson 14c25ca
Add comments explaining the logic in recursive schema code
baileympearson 50c0f28
remove unused import to fix linting
baileympearson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
test/types/community/collection/findX-recursive-types.test-d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { expectError } from 'tsd'; | ||
|
||
import type { Collection } from '../../../../src'; | ||
|
||
/** | ||
* mutually recursive types are not supported and will not get type safety | ||
*/ | ||
interface A { | ||
b: B; | ||
} | ||
|
||
interface B { | ||
a: A; | ||
} | ||
|
||
declare const mutuallyRecursive: Collection<A>; | ||
//@ts-expect-error | ||
mutuallyRecursive.find({}); | ||
mutuallyRecursive.find({ | ||
b: {} | ||
}); | ||
|
||
/** | ||
* types that are not recursive in name but are recursive in structure are | ||
* still supported | ||
*/ | ||
interface RecursiveButNotReally { | ||
a: { a: number; b: string }; | ||
b: string; | ||
} | ||
|
||
declare const recursiveButNotReallyCollection: Collection<RecursiveButNotReally>; | ||
expectError( | ||
recursiveButNotReallyCollection.find({ | ||
'a.a': 'asdf' | ||
}) | ||
); | ||
recursiveButNotReallyCollection.find({ | ||
'a.a': 2 | ||
}); | ||
|
||
/** | ||
* recursive schemas are now supported, but with limited type checking support | ||
*/ | ||
interface RecursiveSchema { | ||
name: RecursiveSchema; | ||
age: number; | ||
} | ||
|
||
declare const recursiveCollection: Collection<RecursiveSchema>; | ||
recursiveCollection.find({ | ||
name: { | ||
name: { | ||
age: 23 | ||
} | ||
} | ||
}); | ||
|
||
recursiveCollection.find({ | ||
age: 23 | ||
}); | ||
|
||
/** | ||
* Recursive optional schemas are also supported with the same capabilities as | ||
* standard recursive schemas | ||
*/ | ||
interface RecursiveOptionalSchema { | ||
name?: RecursiveOptionalSchema; | ||
age: number; | ||
} | ||
|
||
declare const recursiveOptionalCollection: Collection<RecursiveOptionalSchema>; | ||
|
||
recursiveOptionalCollection.find({ | ||
name: { | ||
name: { | ||
age: 23 | ||
} | ||
} | ||
}); | ||
|
||
recursiveOptionalCollection.find({ | ||
age: 23 | ||
}); | ||
|
||
/** | ||
* recursive union types are supported | ||
*/ | ||
interface Node { | ||
next: Node | null; | ||
} | ||
|
||
declare const nodeCollection: Collection<Node>; | ||
|
||
nodeCollection.find({ | ||
next: null | ||
}); | ||
|
||
expectError( | ||
nodeCollection.find({ | ||
next: 'asdf' | ||
}) | ||
); | ||
|
||
nodeCollection.find({ | ||
'next.next': 'asdf' | ||
}); | ||
|
||
nodeCollection.find({ 'next.next.next': 'yoohoo' }); | ||
|
||
/** | ||
* Recursive schemas with arrays are also supported | ||
*/ | ||
interface MongoStrings { | ||
projectId: number; | ||
branches: Branch[]; | ||
twoLevelsDeep: { | ||
name: string; | ||
}; | ||
} | ||
|
||
interface Branch { | ||
id: number; | ||
name: string; | ||
title?: string; | ||
directories: Directory[]; | ||
} | ||
|
||
interface Directory { | ||
id: number; | ||
name: string; | ||
title?: string; | ||
branchId: number; | ||
files: (number | Directory)[]; | ||
} | ||
|
||
declare const recursiveSchemaWithArray: Collection<MongoStrings>; | ||
expectError( | ||
recursiveSchemaWithArray.findOne({ | ||
'branches.0.id': 'hello' | ||
}) | ||
); | ||
|
||
expectError( | ||
recursiveSchemaWithArray.findOne({ | ||
'branches.0.directories.0.id': 'hello' | ||
}) | ||
); | ||
|
||
// type safety breaks after the first | ||
// level of nested types | ||
recursiveSchemaWithArray.findOne({ | ||
'branches.0.directories.0.files.0.id': 'hello' | ||
}); | ||
|
||
recursiveSchemaWithArray.findOne({ | ||
branches: [ | ||
{ | ||
id: 'asdf' | ||
} | ||
] | ||
}); | ||
|
||
// type inference works on properties but only at the top level | ||
expectError( | ||
recursiveSchemaWithArray.findOne({ | ||
projectId: 'asdf' | ||
}) | ||
); | ||
|
||
recursiveSchemaWithArray.findOne({ | ||
twoLevelsDeep: { | ||
name: 3 | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.