How to overwrite incorrect TypeScript type definition installed via @types/package

后端 未结 4 795
悲哀的现实
悲哀的现实 2020-12-02 08:28

Say that I want to use dotenv module in my TypeScript project and install its .d.ts using npm install @types/dotenv --save. Then I realize that the

4条回答
  •  情深已故
    2020-12-02 09:07

    I would check that the version of dotenv and the version of @types/dotenv are aligned, that may be the cause of the function missing.

    If they are then the cleaner way would be to modify the .d.ts yourself.

    In order to do this: npm remove @types/dotenv. Create a folder types on your project. Copy the whole folder dotenv found in node_modules/@types into it.

    Then fix your d.ts in it and modify your tsconfig.json to tell it to also look in your new folder for missing types with typeRoots like this:

    {
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "typeRoots": [
            "./node_modules/@types",
            "./types/",
        ]
    },
    "files": ["./app.ts"]
    }
    

    (Don't forget to add ./node_modules/@types or other types you got with npm that won't be found anymore.)

    Hope it helps!

提交回复
热议问题