typescript:如何扩展现有模块定义?

2024-04-07

我有一个为 extsting npm 包编写的声明文件,但似乎没有声明一种方法,我尝试声明它,但出现错误。请帮帮我。

现有 d.ts 文件的结构:

declare module "mongoose" {
...
  class Document {}
  interface Document extends MongooseDocument, NodeJS.EventEmitter, ModelProperties {
    increment(): this;
    remove(fn?: (err: any, product: this) => void): Promise<this>;
    ...
  }
}

我尝试添加到接口文档方法deleteOne。我的定制.d.ts:

declare module "mongoose" {
  interface Document {
    deleteOne(fn?: (err: any, product: this) => void): Promise<this>;
  }
}

但我仍然收到错误“类型上不存在属性‘deleteOne’”。

如果您需要,这是我的 tsconfig.json:

{
    "compilerOptions": {
      "module": "commonjs",
      "removeComments": true,
      "esModuleInterop": true,
      "moduleResolution": "node",
      "allowJs": true,
      "allowSyntheticDefaultImports": true,
      "pretty": true,
      "resolveJsonModule": true,
      "sourceMap": true,
      "target": "ES2018",
      "outDir": "dist",
      "baseUrl": ".",
      "paths": {
          "*": [
              "node_modules/*"
          ]
      }
    },
    "include": [
      "src/**/*"
    ],
    "exclude": [
      "node_modules",
      "dist",
      "**/*.spec.ts"
    ]
  }

我的 custom.d.ts 文件位于“src/”目录中。


在我的例子中,我必须使用重新导出来合并声明。打字稿 4.0.5

global-axios.d.ts:

export * from 'axios';

declare module 'axios' {
    export interface AxiosRequestConfig {
        myConfigOption?: boolean;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

typescript:如何扩展现有模块定义? 的相关文章

随机推荐