-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: None
-
0
-
None
-
Not Needed
-
None
-
None
-
None
-
None
-
None
-
None
What problem are you facing?
I want to define a generic storage class and use a generic type in the collection definition. Generic constraints do not work for me with filter type definitions.
All my schemas have common properties and I want to use these properties in filters. For example, all schemas have the `_id` property. I define this property in the interface and specify constrain to the generic type to force the type to extend the interface.
The generic type should have the property because this is forced by the constraint. But I can not use the property with a filter.
The following code does not compile.
interface TypeWithId {
_id: number;
{{ }}}
class Storage<T extends TypeWithId> {
constructor(private db: Db) {}
public async find(id: number): Promise<T | null> {
const schemaForGeneric = this.db.collection<T>('a');
const result = await schemaForGeneric.findOne({{
}});
expectType<T | null>(result);
return result;
{{ }}}
{{ }}}
Below is the compilation error.
{{ ❯ npm run check:tsd}}> mongodb@4.2.2 check:tsd
{{ > tsd --version && tsd}}
0.19.0
test/types/community/collection/findX.test-d.ts:303:50
✖ 303:50 No overload matches this call.
The last overload gave the following error.
Argument of type{{
}}is not assignable to parameter of type Filter<T>.
Type{{
}}is not assignable to type{{
{ [Property in Join<NestedPaths<WithId<T>>, ".">]?: Condition<PropertyType<WithId<T>, Property>> | undefined; }}}.
✖ 304:25 Argument of type void is not assignable to parameter of type T | null.
✖ 305:4 Type void is not assignable to type T | null.3 errors{{~/Projects/node-mongodb-native on main !1 took 8s at }}
What driver and relevant dependency versions are you using?
The latest code from the `main` branch.
Steps to reproduce?
- Added my test code to the `test/types/community/collection/findX.test-d.ts` file.
- Run tests with the `npm run check:tsd` command.