{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8335","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8335/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8335/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8335/events","html_url":"https://github.com/Microsoft/TypeScript/issues/8335","id":151376757,"number":8335,"title":"What is the correct way to write a .d.ts file for a node module, which is referenced in package.json?","user":{"login":"0815fox","id":8955528,"avatar_url":"https://avatars.githubusercontent.com/u/8955528?v=3","gravatar_id":"","url":"https://api.github.com/users/0815fox","html_url":"https://github.com/0815fox","followers_url":"https://api.github.com/users/0815fox/followers","following_url":"https://api.github.com/users/0815fox/following{/other_user}","gists_url":"https://api.github.com/users/0815fox/gists{/gist_id}","starred_url":"https://api.github.com/users/0815fox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/0815fox/subscriptions","organizations_url":"https://api.github.com/users/0815fox/orgs","repos_url":"https://api.github.com/users/0815fox/repos","events_url":"https://api.github.com/users/0815fox/events{/privacy}","received_events_url":"https://api.github.com/users/0815fox/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-04-27T12:49:22Z","updated_at":"2016-04-27T19:02:27Z","closed_at":null,"body":"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.\r\n\r\nIn my module path i have package.json with:\r\n```\r\n{\r\n  \"name\": \"parallel-nock\",\r\n  \"version\": \"0.0.1\",\r\n  \"description\": \"Adds a wrapper around nock, which allows to use it more efficiently in parallel tests.\",\r\n  \"main\": \"index.js\",\r\n  \"typings\": \"index.d.ts\",\r\n...\r\n  \"dependencies\": {\r\n    \"bluebird\": \"^3.3.5\",\r\n    \"nock\": \"^8.0.0\"\r\n  }\r\n}\r\n```\r\nindex.js is a very simple module, exporting just a function:\r\n```\r\n'use strict';\r\nvar Promise = require('bluebird');\r\nvar nock = require('nock');\r\n\r\nvar scopePromises = {};\r\n\r\nfunction extendScope(Url) {\r\n  return new Promise(function(resolve){\r\n    scope = Object.create(nock(Url));\r\n    scope.release = function() {\r\n      resolve(scope);\r\n    };\r\n  };\r\n}\r\n\r\nfunction parallelNock(Url) {\r\n  var scopePromise = scopePromises[Url];\r\n  if (scopePromise) {\r\n    scopePromise = scopePromise.then((Scope){\r\n      return extendScope;\r\n    });\r\n  } else {\r\n    scopePromise = extendScope(Url);\r\n  }\r\n  scopePromises[Url] = scopePromise;\r\n}\r\n\r\nmodule.exports = parallelNock;\r\n```\r\nAnd index.d.ts is:\r\n```\r\ndeclare module \"parallel-nock\" { \r\n  import nock = require('nock');\r\n  \r\n  namespace parallelNock {\r\n    export function release():void;\r\n  }\r\n \r\n  function parallelNock (host: string, options?: any): any;\r\n  export = parallelNock;\r\n}\r\n```\r\n+ when I import that module into my project:\r\n```\r\nimport parallelnock = require('parallel-nock');\r\n```\r\n, I get:\r\n```\r\n$ tsc\r\ntest.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.\r\n```\r\n+ I found no \"official\" resource on how to write a correct .d.ts file, especially for a \"module\" which only exports a function.\r\n+ 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.\r\n+ 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.\r\n+ I am confused now."},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/215194561","html_url":"https://github.com/Microsoft/TypeScript/issues/8335#issuecomment-215194561","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/8335","id":215194561,"user":{"login":"mhegazy","id":8000722,"avatar_url":"https://avatars.githubusercontent.com/u/8000722?v=3","gravatar_id":"","url":"https://api.github.com/users/mhegazy","html_url":"https://github.com/mhegazy","followers_url":"https://api.github.com/users/mhegazy/followers","following_url":"https://api.github.com/users/mhegazy/following{/other_user}","gists_url":"https://api.github.com/users/mhegazy/gists{/gist_id}","starred_url":"https://api.github.com/users/mhegazy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhegazy/subscriptions","organizations_url":"https://api.github.com/users/mhegazy/orgs","repos_url":"https://api.github.com/users/mhegazy/repos","events_url":"https://api.github.com/users/mhegazy/events{/privacy}","received_events_url":"https://api.github.com/users/mhegazy/received_events","type":"User","site_admin":false},"created_at":"2016-04-27T19:02:27Z","updated_at":"2016-04-27T19:02:27Z","body":"change your .d.ts file to be a module (i.e. a top level import or export), so it would look like:\r\n\r\n```ts\r\n  import nock = require('nock');\r\n\r\n  namespace parallelNock {\r\n    export function release():void;\r\n  }\r\n\r\n  function parallelNock (host: string, options?: any): any;\r\n  export = parallelNock;\r\n```"}}