question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the correct way to write a .d.ts file for a node module, which is referenced in package.json?

See original GitHub issue

So, i am working on a small module written in JS, but I want to make it easy to use that module in TypeScript as well. It adds a wrapper with some additional functionality around an existing JS module.

In my module path i have package.json with:

{
  "name": "parallel-nock",
  "version": "0.0.1",
  "description": "Adds a wrapper around nock, which allows to use it more efficiently in parallel tests.",
  "main": "index.js",
  "typings": "index.d.ts",
...
  "dependencies": {
    "bluebird": "^3.3.5",
    "nock": "^8.0.0"
  }
}

index.js is a very simple module, exporting just a function:

'use strict';
var Promise = require('bluebird');
var nock = require('nock');

var scopePromises = {};

function extendScope(Url) {
  return new Promise(function(resolve){
    scope = Object.create(nock(Url));
    scope.release = function() {
      resolve(scope);
    };
  };
}

function parallelNock(Url) {
  var scopePromise = scopePromises[Url];
  if (scopePromise) {
    scopePromise = scopePromise.then((Scope){
      return extendScope;
    });
  } else {
    scopePromise = extendScope(Url);
  }
  scopePromises[Url] = scopePromise;
}

module.exports = parallelNock;

And index.d.ts is:

declare module "parallel-nock" { 
  import nock = require('nock');

  namespace parallelNock {
    export function release():void;
  }

  function parallelNock (host: string, options?: any): any;
  export = parallelNock;
}
  • when I import that module into my project:
import parallelnock = require('parallel-nock');

, I get:

$ tsc
test.ts(4,31): error TS2656: Exported external package typings file '/home/mk/work/4.5.1/workspace2/cloud/node_modules/parallel-nock/index.d.ts' is not a module. Please contact the package author to update the package definition.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:38
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

44reactions
mhegazycommented, Apr 27, 2016

change your .d.ts file to be a module (i.e. a top level import or export), so it would look like:

import nock = require('nock');

declare namespace parallelNock {
    export function release():void;
}

declare function parallelNock (host: string, options?: any): any;
export = parallelNock;
18reactions
mhegazycommented, Apr 27, 2016

we have some documentation about exposing types in npm at: http://www.typescriptlang.org/docs/handbook/typings-for-npm-packages.html

that could definitely be better though. we are working on updating the typing acquisition story, so i will leave this issue open until that is done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Creating .d.ts Files from .js files - TypeScript
Run the TypeScript compiler to generate the corresponding d.ts files for JS files; (optional) Edit your package.json to reference the types. Adding TypeScript....
Read more >
Publishing Node modules with TypeScript and ES modules
Publishing Node modules with TypeScript and ES modules · First, create an empty directory and run npm init -y to create a new...
Read more >
Generated definition file (.d.ts) by typescript not working with ...
<reference path="mydefinition.d.ts" /> import { MyInterface } from ... file it will be easier to use in package.json file for npm module.
Read more >
package.json - npm Docs
json which is a map of command name to local file name. When this package is installed globally, that file will be either...
Read more >
Writing a Node.js module in TypeScript - Twilio
You should see a new dist directory that has two files, index.js and index.d.ts . The index.js contains all the logic that we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found