typescriptdefinitelytypedtsd

tsd: install local definition file


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;
}

Solution

  • In your local node package, add a typescript > definition entry in package.json:

    {
      "name": "your-package",
      ...
      "typescript": {
        "definition": "package.d.ts"
      }
    }
    

    Then after installing the package in your project, run the command...

    tsd link
    

    ...which will add a reference to package.d.ts in your project's tsd.d.ts file (reference).


    Also, based on your edit, I would suggest you change your definition file to something like this (note the quotes around my-package):

    declare module "my-package" {
        function myPackage(a: string, b: string): string;
        export = myPackage;
    }
    

    That will make it work with the following code:

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