0

i am trying to use mongodb typings which i installed via

npm install @types/mongodb -D

now i want to use the types in a function like that

export default async function insertOne(collection:any, data:any):Promise<InsertOneWriteOpResult> {
  let db = await state.db
  let col = await db.collection(collection)
  let result = await col.insertOne(data)
  return result
}

i am specifically interested in the Type of InsertOneWriteOpResult. but i am getting the typescript compiler error:

src/utils/mongodb/collection/insert-one.ts|5 col 17 error| Cannot find namespace 'InsertOneWriteOpResult'.

i referenced the typings file in my typings.d.ts

/// <reference path="./../node_modules/@types/mongodb/index.d.ts" />

in this file i can see, that the interface is exported:

export interface InsertOneWriteOpResult {
    insertedCount: number;
    ops: Array<any>;
    insertedId: ObjectID;
    connection: any;
    result: { ok: number, n: number }
}

So my question is, which namespace do i have to use to let typescript check against InsertOptionsWriteOpResult?

1 Answer 1

1

Do not write <reference path="./../node_modules/@types/mongodb/index.d.ts" />.

Instead write it like this

import {InsertOneWriteOpResult} from 'mongodb';

export default async function insertOne(collection: {}, data: {}): Promise<InsertOneWriteOpResult> { 
  ... 
}
Sign up to request clarification or add additional context in comments.

1 Comment

@divramod glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.