日本語

Fixing TS7016 for Untyped npm Packages in TypeScript

​​TypeScript​npm,Troubleshooting

error TS7016: Could not find a declaration file for module 'lodash-es'. appears when TypeScript cannot find a declaration file (.d.ts) for an npm package you imported. The package itself is installed and runs. Installing @types/<package-name>, or placing a declaration file that contains declare module, makes the error go away.

This is the cause and fix when you import a library installed from npm into TypeScript code and get this error from the type check (tsc), or see a red squiggle on the import line in your editor. I reproduced the error in a test environment and included the message as-is. I confirmed that the error disappears after the fix, both with tsc and with the language server the editor uses.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, npm 10.9.9, and TypeScript 7.0.2. I used lodash-es 4.18.1 as the example package without types. For the editor results, I ran the TypeScript language server that editors use for diagnostics (tsc --lsp --stdio) directly, not the editor application itself such as VS Code. I did not test how things look in the VS Code UI, builds through bundlers such as Vite, or TypeScript versions earlier than 7.

The code that causes the error and its message

I ran npm init -y in an empty folder and added "type": "module" to package.json. I installed TypeScript 7.0.2 and lodash-es 4.18.1, then created tsconfig.json with npx tsc --init. The generated tsconfig.json included "strict": true.

The lodash-es folder has no .d.ts files, and its package.json has no types field. There is no node_modules/@types folder.

In the following src/index.ts, I imported debounce from lodash-es.

ts
import { debounce } from "lodash-es";

const save = debounce((text: string) => {
  console.log(`saved: ${text}`);
}, 300);

save("hello");

Running npx tsc --noEmit printed the two lines below and exited with code 1. (In the test, I saved the output to a file.) /path/to/app is the project location. I replaced the paths from the actual output with this placeholder, and the same applies below.

console
$ npx tsc --noEmit
src/index.ts(1,26): error TS7016: Could not find a declaration file for module 'lodash-es'. '/path/to/app/node_modules/lodash-es/lodash.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/lodash-es` if it exists or add a new declaration (.d.ts) file containing `declare module 'lodash-es';`

With --pretty, the output shows the relevant line of code, with a squiggle under "lodash-es" (colors omitted).

text
src/index.ts:1:26 - error TS7016: Could not find a declaration file for module 'lodash-es'. '/path/to/app/node_modules/lodash-es/lodash.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/lodash-es` if it exists or add a new declaration (.d.ts) file containing `declare module 'lodash-es';`

1 import { debounce } from "lodash-es";
                           ~~~~~~~~~~~


Found 1 error in src/index.ts:1

The first line means: "No declaration file was found for lodash-es. node_modules/lodash-es/lodash.js implicitly becomes type any." TypeScript did find the JavaScript body (lodash.js), but there is no type information for it. The second line is the suggested fix: install @types/lodash-es if it exists, or add a .d.ts file containing declare module 'lodash-es';.

Even with a type-check error, the program still runs. Running node src/index.ts on Node.js 22.23.4 printed saved: hello and exited with code 0. (According to the Node.js documentation, from v22.18.0 onward, running .ts files by stripping the type annotations is enabled by default.)

In the editor, the import line is an error and the value is any

I had the language server that the editor uses open the same file. It returned one diagnostic, with the same text as tsc.

text
diagnostics (1):
  1:26-1:37 severity=1 source=ts code=7016 Could not find a declaration file for module 'lodash-es'. '/path/to/app/node_modules/lodash-es/lodash.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/lodash-es` if it exists or add a new declaration (.d.ts) file containing `declare module 'lodash-es';`
hover 3:7: const save: any

1:26-1:37 is the range of "lodash-es" on line 1, including the quotes. In the editor, a red squiggle appears there. severity=1 is the value that the Language Server Protocol specification uses for errors. When I hovered over save on line 3, the tooltip showed const save: any, meaning the value returned by debounce has no type either.

Turning off noImplicitAny removes the error, but the types are still missing

This error comes from the noImplicitAny setting in tsconfig.json. Setting strict: true enables it along with other checks. It turns the case where TypeScript cannot infer a type and falls back to any into an error.

To check this, I added src/typo.ts, which deliberately passes a number to a function that takes a string.

ts
import { debounce } from "lodash-es";

const count = debounce((text: string) => text.length, 300);
count(123);

Running npx tsc --noEmit --noImplicitAny false printed nothing and exited with code 0. Not only did TS7016 disappear, but the count(123) mistake was also not reported. The language server cannot take command-line options, so I wrote "noImplicitAny": false into tsconfig.json and then opened the file. The TS7016 diagnostic came back with severity=4 (a hint in the LSP specification), and hovering over save still showed const save: any.

Turning off noImplicitAny makes the error message go away, but the package still has no types. To fix it properly, add type information as described in the next section.

Install the @types package

For some packages, the type declarations are published separately on npm as their own package. According to the TypeScript Handbook section on finding declaration files, the name is usually the original package name with @types/ in front. For lodash, the handbook says that npm install --save-dev @types/lodash provides the declarations.

You can check whether a package exists with npm view.

console
$ npm view @types/lodash-es version
4.17.12

If you specify a name that does not exist, the output ends with npm error code E404.

I installed it as a development dependency with npm install -D @types/lodash-es@4.17.12. The devDependencies section of package.json then looked like this:

json

"devDependencies": {
    "@types/lodash-es": "^4.17.12",
    "typescript": "^7.0.2"
  },

After this, running npx tsc --noEmit printed nothing and exited with code 0. The language server also returned zero diagnostics, and hovering over save showed const save: DebouncedFunc<(text: string) => void>.

I placed src/typo.ts again and ran it. This time, the mistake was caught.

console
$ npx tsc --noEmit
src/typo.ts(4,7): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

Also, the tsconfig.json generated by tsc --init contains "types": [], but you can leave it as is. According to the description of types, this setting only controls which types are loaded without an import, such as global types like Node.js's process. The @types for a package you import is used regardless of this setting. In my test, the error disappeared with "types": [] still in place.

If there are no @types, write a declare module

For a package with no published @types, you write the declaration file yourself. There are two approaches.

Write one line to clear the error

Following the suggestion in the error message, I wrote this single line in src/lodash-es.d.ts.

ts
declare module "lodash-es";

npx tsc --noEmit then printed nothing and exited with code 0. The language server also returned zero diagnostics.

However, hovering over save still showed const save: any. The TypeScript 2.0 release notes say that for a shorthand ambient module declaration, which has no body, "all imports from the module will have the any type." In my test, I also placed src/typo.ts and ran npx tsc --noEmit, and it exited with code 0. The error goes away, but no type checking is done.

Write types only for the functions you use

You do not need to write types for the whole package. If you write types only for the functions you use, those parts are type-checked. I rewrote src/lodash-es.d.ts like this:

ts
declare module "lodash-es" {
  export function debounce<Args extends unknown[]>(
    func: (...args: Args) => unknown,
    wait?: number,
  ): (...args: Args) => void;
}

npx tsc --noEmit exited with code 0, and hovering over save showed const save: (text: string) => void. When I added src/typo.ts, the same TS2345 error appeared as when I installed @types.

If you import a function that is not declared here, you get a type error. When I added import { throttle } from "lodash-es", this error appeared. As you use more functions, add them to the declaration.

console
src/throttle.ts(1,10): error TS2305: Module '"lodash-es"' has no exported member 'throttle'.

One more point: do not write import or export at the top level of a declaration file. In my test, adding the single line export {}; at the top of src/lodash-es.d.ts brought back TS7016. According to the TypeScript documentation on ambient modules, if a file has a top-level import or export, a declare module inside it is not a declaration of a new module. It is treated as a way to add types to a module that already exists (module augmentation).

If the package is not installed, you get TS2307

TS7016 means the package was found but has no types. If the package itself cannot be found, you get a different error code. When I imported lodash-es-not-installed, which is not in node_modules, the message was:

console
src/missing.ts(1,26): error TS2307: Cannot find module 'lodash-es-not-installed' or its corresponding type declarations.

If you see TS2307, first check the package name for typos and whether you forgot to run npm install.

If the message continues with "There are types at …", the problem is in the package's configuration

The second line of the TS7016 message may not be the Try npm i ... suggestion. Instead, it may read as follows. I triggered this with a local package called demo-pkg that I created for the test.

console
src/demo.ts(1,23): error TS7016: Could not find a declaration file for module 'demo-pkg'. '/path/to/demo-pkg/dist/index.js' implicitly has an 'any' type.
  There are types at '/path/to/demo-pkg/types/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'demo-pkg' library may need to update its package.json or typings.

/path/to/demo-pkg is where I placed demo-pkg. The same run also showed the TS7016 for lodash-es, but I have left it out here.

The message means: "The types are at types/index.d.ts, but they cannot be found when following the exports field in package.json. The demo-pkg library may need its package.json or typings fixed." The package.json of demo-pkg was as follows:

json
{
  "name": "demo-pkg",
  "version": "1.0.0",
  "type": "module",
  "types": "./types/index.d.ts",
  "exports": {
    ".": "./dist/index.js"
  }
}

According to the TypeScript module resolution documentation, when moduleResolution is node16, nodenext, or bundler, files are located by following exports. The top-level types field is read only when exports is not consulted. The tsconfig.json generated by tsc --init has "module": "nodenext", and with this setting moduleResolution also becomes nodenext, so exports was used.

When I added a types condition to the package's exports, the TS7016 for demo-pkg disappeared.

json
{
  "name": "demo-pkg",
  "version": "1.0.0",
  "type": "module",
  "types": "./types/index.d.ts",
  "exports": {
    ".": {
      "types": "./types/index.d.ts",
      "default": "./dist/index.js"
    }
  }
}

This is the fix on the package author's side. As a user of the package, you can work around it the same way as in the previous section, by writing a declaration file. Before I fixed the package.json, placing the following src/demo-pkg.d.ts also removed the TS7016 for demo-pkg.

ts
declare module "demo-pkg" {
  export function greet(name: string): string;
}

​Posted by 86