【问题标题】:Error when using Typescript MongoDb Typings (Cannot find namespace)使用 Typescript MongoDb Typings 时出错(找不到命名空间)
【发布时间】:2017-04-24 07:13:29
【问题描述】:

我正在尝试使用通过

安装的 mongodb 类型
npm install @types/mongodb -D

现在我想在这样的函数中使用类型

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
}

我对 InsertOneWriteOpResult 的类型特别感兴趣。 但我收到打字稿编译器错误:

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

我在 typings.d.ts 中引用了类型文件

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

在这个文件中我可以看到,接口被导出:

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

所以我的问题是,我必须使用哪个命名空间来让 typescript 检查 InsertOptionsWriteOpResult?

【问题讨论】:

    标签: node.js mongodb typescript typescript-typings


    【解决方案1】:

    不要写&lt;reference path="./../node_modules/@types/mongodb/index.d.ts" /&gt;

    改为这样写

    import {InsertOneWriteOpResult} from 'mongodb';
    
    export default async function insertOne(collection: {}, data: {}): Promise<InsertOneWriteOpResult> { 
      ... 
    }
    

    【讨论】:

    • @divramod 很高兴为您提供帮助 :)
    猜你喜欢
    • 1970-01-01
    • 2017-02-15
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2020-02-10
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多