Skip to content

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

Closed
@0815fox

Description

@0815fox

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.
  • I found no "official" resource on how to write a correct .d.ts file, especially for a "module" which only exports a function.
  • I found [http://stackoverflow.com/questions/24029462/how-to-you-write-a-typescript-definition-file-for-a-node-module-that-exports-a-f](this on stackoverflow) but it seems not to work.
  • I oriented on the [https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/nock/nock.d.ts](typings file from the nock module I used) on DefinitelyTyped, which seems to do something very similar.
  • I am confused now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    @typesRelates to working with .d.ts files (declaration/definition files) from DefinitelyTypedDocsThe issue relates to how you learn TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions