6

I know this has been asked an answered elsewhere since at least 2017, but I can't make this work. I have noImplicitAny: true in my tsconfig.json in my project. I'm trying to use clamscan which is neither natively typed nor available in @types. My question is not specific to this package.

So when I import NodeClam from 'clamscan' in code.ts I get the following error:

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

So I created a clamscan.d.ts file with:

declare module 'clamscan' {
  // the declarations I use
}

I also tried simply declare module 'clamscan';. In both cases I get:

Invalid module name in augmentation. Module 'clamscan' resolves to an untyped module at '.../node_modules/clamscan/index.js', which cannot be augmented.ts(2665)

On the other hand, declare module '*'; is accepted by the compiler but isn't quite what I want (and doesn't solve the original error anyways). So, how can I type an untyped foreign npm module ?

I understand that I can contribute to DefinitlyTyped or the clamscan package, but the question is about annotating/augmenting the package locally just for my project.

4
  • 1
    I suspect typescript can't find your declarations file. Try putting in at the path src/@types/clamscan/index.d.ts or src/@types/clamscan.d.ts. Or editing the typeRoots property in your .tsconfig to include the path where you have stored it. I get the "invalid module..." error if I've declared the type after the module has already been imported. Commented Sep 30, 2020 at 21:07
  • Thanks for the tip. Can't make it work anywhere in src/ but I put it in node_modules/@types/clamscan/index.d.ts for now. Gonna play with typeRoots to see if I can do better. Commented Oct 2, 2020 at 10:34
  • Every solve this? Commented Feb 7, 2021 at 20:40
  • @DouglasGaskell not really, I worked in node_modules/@types, which is not ideal obivously, so I ended up having my type definitions merged in the upstream package, so that it can be useful to others - so I guess problem solved, but I don't have a technical solution for my original issue Commented Feb 12, 2021 at 9:08

2 Answers 2

2

Try this:

declare module 'clamscan' {
   import * as Clamscan from 'clamscan';
  export default Clamscan
}

or create a single file with only this inside "clamscan.d.ts"

declare module 'clamscan' {
       import * as Clamscan from 'clamscan'; // or import Clamscan from "clamscan" or import {Clamscan} from "clamscan" . It depends of how the import is handled
    }

If this doesn't work take a look at this solution

Sign up to request clarification or add additional context in comments.

Comments

0

Had to do this for it to work with digest-stream :

declare module "digest-stream" {
  export default function digestStream(algorithm: string, inputEncoding: string, digestEncoding?: any, options?: any, listenerFn?: any): any
}

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.