maccoda
maccoda

Reputation: 98

Adding Typescript declaration for package with inner modules

I am wanting to add types to a component library written purely in Javascript that I am not currently able to add directly to. I have followed this example for adding declarations to third party packages, which almost got me there. The current issue I am having is the package structure.

The package is located in node_modules as @company/company-widgets, I am not exactly certain how to handle the inner module as tsc is unable to find and associate the declaration files I have added. The compiler's recommendation is Try `npm install @types/company__company-widgets` if it exists or add a new declaration (.d.ts) file containing `declare module 'company__company-widgets';`

I have tried the following:

  1. Modifying tsconfig.json with "typeRoots": ["@types", "./src/@types"] and adding anindex.d.ts` file with the module declaration specified in the compiler message
  2. With the above tsconfig.json changes, adding directories under src/@types to match that of node_modules
  3. I also changed the declaration to have a module named company and a namespace of __company-widgets
  4. I also got desparate and started messing around with node_modules by placing a declaration in there

None of these have got the declaration found unfortunately, so I have been thinking it is unable to associate the module name in the declaration to the package name. Any suggestions or answers would be greatly appreciated. Thanks!

Upvotes: 0

Views: 886

Answers (1)

Matt McCutchen
Matt McCutchen

Reputation: 30949

The compiler's recommendation is wrong: the module declaration should be declare module '@company/company-widgets';, not declare module 'company__company-widgets';. I fixed this recently and the fix should be in TypeScript 3.1.

Upvotes: 1

Related Questions