Using a JavaScript library (without type declarations) in a TypeScript project.

Steve Ruiz
2 min readJan 9, 2020

--

TS7016 Error message
uuuggghhhh

Let’s say you want to use a library in your TypeScript project. You install the package, import it into a .ts file — but then immediately run into a TS7016 error.

Could not find a declaration file for module 'my-untyped-module'. '.../node_modules/my-untyped-module/index.js' implicitly has an 'any' type.Try `npm install @types/my-untyped-module` if it exists or add a new declaration (.d.ts) file containing `declare module 'isomath';`  TS7016

This means that the module does not include TypeScript typings.

Don’t give up yet, though — you can still use the library in your TypeScript project. Here’s how.

Plan B: Check Definitely Typed

Even though the author didn’t provide typings, there’s still a chance that the wider TypeScript community has stepped in to provide them through the Definitely Typed repository. This is common for useful older packages.

Following the instructions in your error message, run:

npm i @types/my-untyped-module

If the types install, then problem solved. ✨ Thanks, Definitely Typed!

However, you might get another error message instead:

npm ERR! 404 Not Found: @types/my-untyped-module@latest

This means that the types don’t exist at Definitely Typed. You’re out of luck — the author hasn’t provided types and no one else has, either.

While you’re not going to get any of the benefits of typing for this library, you can still use it in your project! On to Plan C.

Plan C: Create a declaration file

While TypeScript is used to knowing everything about the code in your project, it doesn’t have to. In order to let you proceed, TypeScript just needs to know that your module exists.

And you do that through a declarations file.

In the root folder of your project, create a new file called decs.d.ts.

In this file, write:

declare module "my-untyped-module"

So far, we’ve really just been following error messages. Now comes the part that the error messages leave out: making sure that TypeScript can find this file.

Include the declarations file

In the root of your TypeScript project, you should have a tsconfig.json file. Open that up and find the property "include". This "include" array tells TypeScript where it should “look” for TypeScript files.

{
...
"include": [
"src"
]
}

You need to make sure that your decs.d.ts is included here.

You have two choices:

  • Move your decs.d.ts file into whatever folder you see in the "include" array. (In the example above, I’d move my file into my project’s src folder.)

OR

  • Leave our decs.d.ts in the root of your project and add the file to the "include" array:
{
...
"include": [
"decs.d.ts"
]
}

Either choice will solve your error. You won’t have any types for the module (it’ll just be one big any) but you won’t have any errors, either.

Good luck!

--

--