TypeScript:如何为已安装的npm包定义自定义类型? [英] TypeScript: How to define custom typings for installed npm package?

查看:261
本文介绍了TypeScript:如何为已安装的npm包定义自定义类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在TypeScript中使用 rx节点

I like to use rx-node within TypeScript

import RxNode from 'rx-node';

我使用npm安装了 rx-节点

$ npm install rx-node --save

我搜索了类型定义,但没有任何结果

I searched for type definitions, but without any result

$ typings search rx-node
No results found for search

如何为已安装的npm模块 rx节点定义自定义类型定义?我应该在哪里存储类型定义文件?如何配置TypeScript( tsconfig.json typings.json )?

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)?

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

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

我安装了 @ reactivex/rxjs

I installed @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.

推荐答案

请注意,这是关于使用键入,而建议通过

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

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

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

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

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

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

declare module RxNode {
    export interface ...
}

然后使用命令安装

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


或手动:在 typings.json 中添加对此键入文件的引用:


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

然后运行typings install

这篇关于TypeScript:如何为已安装的npm包定义自定义类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆