为模块编写定义文件导出一个返回模块的函数

Write definition files for modules exports a function returning a module

我最近正在为 node.js 模块 "mongoose-bird" 编写定义文件。该模块导出一个返回模块的函数。借助互联网上的指南,我写了这个:

declare module "mongoose-bird" {
  export = () => MongooseAsync;
  module MongooseAsync {
...

但是我发现使用下面的代码,类型系统无法正常工作:

import mongoose_bird = require('mongoose-bird');
var mongoose = mongoose_bird();
...
export interface IUser extends mongoose.Document {

由于 tsc 报告错误 TS2503

error TS2503: Cannot find namespace 'mongoose'.

error TS2503: Cannot find namespace 'mongoose'.

因为您使用 import 使您的文件成为一个模块(与全局命名空间断开连接)。

推荐 .d.ts 用于(手波浪)声明,仅与相应的 .ts 用于实施。