Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: How to define custom typings for installed npm package?

I like to use rx-node within TypeScript

import RxNode from 'rx-node'; 

I installed rx-node using npm

$ npm install rx-node --save 

I searched for type definitions, but without any result

$ typings search rx-node No results found for search 

How can I define custom type definitions for the installed npm module rx-node? Where should I store the type definition file? How to configure TypeScript (tsconfig.json and typings.json)?

Edit: Thanks to Aleksey L. and David Bohunek I achieved to define a rx-node.d.ts that looks like this

declare module "rx-node" {   import {Observable} from '@reactivex/rxjs';   import {ReadLine} from "readline";    function fromReadLineStream(stream: ReadLine): Observable<string> } 

I installed @reactivex/rxjs

npm install --save @reactivex/rxjs 

Since I got an error

node_modules\@reactivex\rxjs\dist\cjs\Observable.d.ts (10,66): Cannot find name 'Promise'. (2304) 

I changed the target in tsconfig.json to es6.

like image 253
maiermic Avatar asked Jun 05 '16 12:06

maiermic


People also ask

How do I add TypeScript support to NPM package?

You can use npm to install TypeScript globally, this means that you can use the tsc command anywhere in your terminal. To do this, run npm install -g typescript . This will install the latest version (currently 4.7).

Do npm packages work with TypeScript?

An NPM module must supply JavaScript (not TypeScript) code. It's important to provide JavaScript so the modules work with JavaScript projects. A module specifies an entry point with the main property in package.


2 Answers

Beware, this is old question (2016) regarding managing definitions using typings which is deprecated in favor of installing them straight via npm


You can add custom definitions to typings.json. For example having following folder structure:

/typings     /custom         rx-node.d.ts     /global         ... 

where rx-node.d.ts is your custom typings file. For example:

declare module RxNode {     export interface ... } 

Then install with a command

typings install file:typings/custom/rx-node.d.ts --save --global 

Or manually: in typings.json add reference to this typings file:

{     "name": "TestName",     "version": false,     "globalDependencies": {         "rx-node": "file:typings/custom/rx-node.d.ts"     } } 

And run typings install

like image 71
Aleksey L. Avatar answered Oct 17 '22 02:10

Aleksey L.


Create a new file, called it rx-node.d.ts and make sure it's referenced in your tsconfig.json either directly or from another file, for example typings/index.d.ts. Then you can start with something like this:

///<reference path="rx.d.ts" />  declare namespace RxNode  {      export interface Emitter<T>{      }      export function toEventEmitter<T>(observable: Rx.Observable<T>, eventName: string): Emitter<T>; } 

This question/answer might be helpuful: How to write d.ts file from existing .js file?

If you create a nice, high quality typings definition file, contribute to https://github.com/DefinitelyTyped/DefinitelyTyped

like image 28
David Bohunek Avatar answered Oct 17 '22 03:10

David Bohunek