Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
2adc7cd
commit dd5195a
Showing
4 changed files
with
253 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 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