Tsd: install local definition file

I have a local node package written in TypeScript that I want to use in my actual project. Using npm, I can install local packages as follows:

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

Or:

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

This installs the necessary .js files in the node_modules directory. It also created the generated .d.ts file inside this package, which I would like to install in my project (automatically linking it to 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 using a repository?

UPDATE:

I can just copy the d.ts file to the sample directory and my text editor (for me this is Sublime Text with the TypeScript plugin) he can find the ad. The catalog layout looks 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 had a problem when exporting a single function to module.exports ( exports = function... in TypeScript). In this case, the exported function is “anonymous” and is not even specified in the d.ts file, so I need to manually edit it.

My test case:

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

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

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

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

My package should be used this way in my project:

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

However, tsc cannot find myPackage , although my-package.d.ts been copied to the sample folder. I need to edit this file so that it looks like this:

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

Or even better for require() to function properly:

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

In the local node package, add the typescript > definition entry in package.json :

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

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

 tsd link 

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


Also, based on your editing, I would suggest you change the 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; } 

This will make it work with the following code:

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

Even if the trick with package.json works, I prefer the tools created for this (tsd or typings).

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

I think the same with tsd :)

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

+8
source

As with TypeScript 1.6, you can reference your type definition file from package.json and TypeScript, the module resolution should be able to dig up the type definition.

In your package.json file (your local npm module) add the entry "typings", for example.

 { "name": "my-package", "typings": "./relative/path/to/my-package.d.ts" } 

This way you don’t have to bother with TSD at all.

See TypeScript Wiki: https://github.com/Microsoft/TypeScript/wiki/Typings-for-npm-packages

+1
source

Source: https://habr.com/ru/post/1239689/


All Articles