0

I'm importing a Javascript package from npm that doesn't have a Typescript definition file anywhere. I'd like to create a local definition file that I'll eventually PR to DefinitelyTyped or the project itself.

I can't figure out where/how to create and place the definition file such that it will be found locally.

I could copy it into the node_modules/the-package directory, but it'll get clobbered during otherwise safe npm operations.

From observing tsc --traceResolution, I don't see a good way to do this, unless there's some way to use types or typings that I haven't figured.

2 Answers 2

5

One of the ways I failed in this was to create a local *.d.ts file like this one-line modification of @Saravana's example:

import {Component} from 'react'

declare module 'lodash' {

  interface Lodash {
      map: Function
  }

  var _: Lodash;
  export = _;
}

This evokes the cryptic error message

Error:(3, 16) TS2665:Invalid module name in augmentation. Module 'lodash' resolves to an untyped module at 'my-project/node_modules/lodash/lodash.js', which cannot be augmented.

The error made me think that this approach must be wholly wrong, when it's really just complaining about a trivial, easy-to-fix error. When you import in a file, it converts the file into a module, and when Typescript sees a declare module inside a module, it assumes you're trying to enhance an existing definition. The fix is trivial: just move the import inside the declare block, like so:

declare module 'lodash' {

  import {Component} from 'react'

  interface Lodash {
      map: Function
  }

  var _: Lodash;
  export = _;
}

If you use an IDE with auto-import, it will break your definition files in this way every time - remember to move the import!

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

Comments

1

You can just create a local file with *.d.ts and create a module with the same name as your library.

For example, if you were using lodash, you can create typings for it by creating a d.ts file:

declare module "lodash" {

    interface Lodash {
        map: Function
    }

    var _: Lodash;
    export = _;
}

See the documentation on creating declarations.

1 Comment

How/where are you declaring your definition files? Can you update your question with a reproducible example?

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.