Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
943 views
in Technique[技术] by (71.8m points)

node.js - Having error "Module 'name' resolves to an untyped module at..." when writing custom TypeScript definition file

I can't find TypeScript definition @type/{name} for one of my installed NodeJS packages, so I attempt to write a d.ts file for it, and put the file in {project root}ypings folder. This is how I do:

// My source code: index.ts
import Helper from 'node-helper-lib';


// My definition: ypings
ode-helper-lib.d.ts
declare....(something else)

declare module 'node-helper-lib' {
   class Helper { ... }
   export = Helper;
}

However, Visual Studio Code keeps yielding this error and puts red line under declare module 'node-helper-lib':

[ts] Invalid module name in augmentation. Module 'node-helper-lib' resolves to an untyped module at '{project path} ode_modules ode-helper-libindex.js', which cannot be augmented.

Isn't it legit that because the library is untyped, so I should be allowed to add typing to it?

UPDATE:

I am using:

  • TypeScript: 2.1.4
  • Visual Studio Code: 1.9.1
  • Node JS: 6.9.4
  • Windows 10 x64
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The actual solution is given in a comment by @Paleo in @hirikarate's answer:

Imports should be declared inside the module declaration.

Example:

declare module 'node-helper-lib' {
   import * as SomeThirdParty from 'node-helper-lib';
   interface Helper {
       new(opt: SomeThirdParty.Options): SomeThirdParty.Type
   }
   export = Helper;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...