How to overwrite incorrect TypeScript type definition installed via @types/package
Solution 1:
You can patch @types/foo
locally for your app by patch-package.
-
Run
npm i -D patch-package
-
Simply modify
node_modules/@types/foo
to suit your needs. -
Run
npx patch-package @types/foo
. This creates a diff file inpatches/
that records the changes made from the last step. -
Add
"scripts": {"postinstall": "patch-package"}
inpackage.json
. This make patches to be applied each time people runnpm install
.
Solution 2:
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!
Solution 3:
I would copy the declaration files from DefinitelyTyped, modify them locally, send a PR to DefinitelyTyped, and then follow the advice given on the following question to use the changes immediately: How can I write and use custom declaration files that don't exist on DefinitelyTyped?
Sending updates to DefinitelyTyped
- Head over to the DefinitelyTyped repo: https://github.com/DefinitelyTyped/DefinitelyTyped/
- Clone your fork locally. (often just
git clone https://github.com/YourUserName/DefinitelyTyped/
) - Create a branch for your updates (e.g.
git branch changes-to-xyz
) - Make changes to the package you're interested in.
- Add and commit files. (
git add types; git commit
) - Then push them to your fork of DefinitelyTyped (
git push -u origin changes-to-xyz
)
Using those updates locally
- Create a
local-types
folder in your project. - Copy the DefinitelyTyped folder (let's call it
xyz
) you modified and intolocal-types/xyz
. - From
local-types/xyz
, runnpm init --scope types --yes
. - From the root of your project, run
npm install local-types/xyz
That's it!