TypeScript:如何定义安装npm包的自定义types?

我喜欢在TypeScript中使用rx-node

import RxNode from 'rx-node'; 

我使用npm安装了rx-node

 $ npm install rx-node --save 

我search了types定义,但没有任何结果

 $ typings search rx-node No results found for search 

如何为已安装的npm模块rx-node定义自定义types定义? 我应该在哪里存储types定义文件? 如何configurationTypeScript( tsconfig.jsontypings.json )?

编辑:感谢Aleksey L.和大卫Bohunek我实现了定义一个rx-node.d.ts ,看起来像这样

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

我安装了@reactivex / rxjs

 npm install --save @reactivex/rxjs 

因为我有一个错误

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

我将tsconfig.json中的目标更改es6

您可以将自定义添加到typings.json 。 例如具有以下文件夹结构:

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

其中rx-node.d.ts是您的自定义types文件。 例如:

 declare module RxNode { export interface ... } 

然后用命令安装

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

或者手动:在typings.json中添加对这个types文件的引用:

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

并运行typestypings install

创build一个新文件,将其命名为rx-node.d.ts ,并确保它在tsconfig.json中直接引用或从其他文件引用,例如typings/index.d.ts 。 那么你可以从这样的事情开始:

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

这个问题/答案可能是有帮助的: 如何从现有的.js文件写入d.ts文件?

如果你创build一个不错的,高质量的types定义文件,贡献https://github.com/DefinitelyTyped/DefinitelyTyped