20

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.

4
  • Can you not use the standard @types/mongoose in your code? npmjs.com/package/@types/mongoose Commented Feb 3, 2020 at 21:53
  • 1
    @skyhavoc it is already @types/mongoose. And also it is important to me to learn how to declare, not just use module Commented Feb 4, 2020 at 5:51
  • Could it be because your document interface is not extending? interface Document extends MongooseDocument, NodeJS.EventEmitter, ModelProperties? Commented Feb 4, 2020 at 11:25
  • @hazardous I tried to extend, but got the same result - error Commented Feb 4, 2020 at 11:26

4 Answers 4

25

I had to use reexport in my case to merge declarations. typescript 4.0.5

global-axios.d.ts:

export * from 'axios';

declare module 'axios' {
    export interface AxiosRequestConfig {
        myConfigOption?: boolean;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! This worked great for me as well. Needed to add a missing type for mixpanel.
5

OK! Now I know this is expected behavior of ts-node: https://github.com/TypeStrong/ts-node#missing-types

I have configured paths settings in tsconfig.json, and now everything is working:

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

Comments

1

defining the Mongoose interface

// types/mongoose/index.d.ts
declare module 'mongoose' {
  namespace Mongoose {
    export interface MyInterface {
      name: number;
    }
  }
}

usage

// app.ts
import mongoose, { Mongoose } from 'mongoose';
const a: Mongoose.MyInterface = {
  name: 122
};

I have also added "typeRoots": ["./node_modules/@types", "./server/types"], to my tsconfig file

does this help

1 Comment

it is not worked, but it was a good hint to me - to change my tsconfic file
0

In my case, there were two files named the same: theme.ts and theme.d.ts, and changing theme.d.ts to something different worked. 🤷

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.