【问题标题】:Typing mongoose.connect输入 mongoose.connect
【发布时间】:2021-03-08 10:04:29
【问题描述】:

我正在尝试为此寻找一种类型:

export const connectToDatabase = ()/* here */ => mongoose.connect(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

到目前为止,我的代码编辑器建议这样做:

export const connectToDatabase = ():
    Promise<typeof mongoose> => mongoose.connect(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

这很愚蠢。我不希望类型为 Promise&lt;typeof mongoose&gt; 并且它不可能是所需的解决方案。那么这里的正确类型是什么?我能找到的只是ConnectionUseDbOptionsConnectionOptions,这不起作用。那么这里的正确类型是什么?

【问题讨论】:

  • 你用的是什么版本的猫鼬?
  • @Anatoly "mongoose": "^5.10.4",

标签: javascript typescript mongoose types


【解决方案1】:

根据index.d.ts中的@types/mongoose

type Mongoose = typeof mongoose;
...
export function connect(uris: string, options: ConnectionOptions, callback: (err: mongodb.MongoError) => void): Promise<Mongoose>;
export function connect(uris: string, callback: (err: mongodb.MongoError) => void): Promise<Mongoose>;
export function connect(uris: string, options?: ConnectionOptions): Promise<Mongoose>;

所以返回类型肯定是Promise&lt;Mongoose&gt;Promise&lt;type of mongoose&gt;

【讨论】:

  • 这样更好。谢谢!
猜你喜欢
  • 2018-09-15
  • 2020-07-01
  • 1970-01-01
  • 2015-06-12
  • 2023-03-20
  • 1970-01-01
  • 2019-06-27
  • 2021-01-26
  • 2021-06-23
相关资源
最近更新 更多