tsd: install local definition file

Simon picture Simon · Jan 2, 2016 · Viewed 14.9k times · Source

I have a local node package written in TypeScript, which I want to use in my actual project. Using npm, I can install local packages like this:

$ npm install --save /path/to/package

Or:

$ npm install --save /path/to/package.tar.gz

This installs the required .js files in the node_modules directory. There's also a generated .d.ts file within that package, which I'd like to install to my project (automatically linking it in typings/tsd.d.ts). But using the following command has no effect:

$ tsd install /path/to/package/package.d.ts --save

It says >> zero results. So, what is the way to install local definition files without the need of a repository?

UPDATE:

I can simply copy my d.ts file into the typings directory and my text editor (for me it's Sublime Text with the TypeScript plugin) it's able to find the declaration. The directory layout is something like this:

/my-project/
  /typings/
    tsd.d.ts - auto-generated by `tsd install`
    node/ - I've installed the node definitions
    my-package.d.ts - copied or symlinked file
  my-project.ts - I'm working here

However I've got an issue when exporting the only function in module.exports (exports = function... in TypeScript). In this case, the exported function is kinda 'anonymous' and isn't even named in the d.ts file, so I need to edit it manually.

My test case:

'my-package' provides a single function, usually imported as 'myPackage':

export = function myPackage(a: string, b: string) { return a + ' ' + b; };

declaration is set to true in tsconfig.json, so the tsc command generated a my-package.d.ts file:

declare var _default: (a: string, b: string) => string;
export = _default;

My package is supposed to be used like this in my project:

import myPackage = require('my-package');
myPackage('foo', 'bar');

However, tsc can't find myPackage, even though my-package.d.ts was copied into the typings folder. I need to edit that file so it looks like this:

declare var myPackage: (a: string, b: string) => string;
//export = _default; - not needed

Or even better for a correct functioning require():

declare module 'my-package' /* this is the string passed to require() */ {
    export = function(a: string, b: string): string;
}

Answer

maxime1992 picture maxime1992 · Feb 9, 2016

Even if the trick with package.json is working, I rather prefer the tools made for that (tsd or typings).

I just found the answer for Typings :
typings install --save --ambient file:./node_modules/.../file.d.ts

I think it's the same with tsd :)

EDIT:
Since TypeScript 2.0 typings is useless.
Just run npm i --save-dev @types/some-library