typescript: How to extend existing module definition?

0

Issue

I have a declaration file written for extsting npm package, but seems like one method was not declared, I try to declare it, but get an error. Help me please.

structure of existing d.ts file:

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

I try to add to interface Document method deleteOne. My custom.d.ts:

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

But still I get an error "Property ‘deleteOne’ does not exist on type".

Here is my tsconfig.json if you need:

{
    "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"
    ]
  }

My custom.d.ts file located in ‘src/’ dir.

Solution

OK! Now I know this is expected behavior of ts-node:
https://github.com/TypeStrong/ts-node#help-my-types-are-missing
I have configured paths settings in tsconfig.json, and now everything is working:

    "paths": {
        "mongoose": ["src/custom.d.ts"],
        "*": [
            "node_modules/*"
        ]
    }

Answered By – Maxim

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More