What is the correct way to write a .d.ts file for a node module, which is referenced in package.json?
See original GitHub issueSo, 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.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:38
- Comments:11 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
change your .d.ts file to be a module (i.e. a top level import or export), so it would look like:
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.