TypeScript:如何为已安装的 npm 包定义自定义类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37641960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript: How to define custom typings for installed npm package?
提问by maiermic
I like to use rx-nodewithin TypeScript
我喜欢在 TypeScript 中使用rx-node
import RxNode from 'rx-node';
I installed rx-nodeusing npm
我使用 npm安装了rx-node
$ 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.jsonand typings.json)?
如何为已安装的 npm 模块rx-node定义自定义类型定义?我应该在哪里存储类型定义文件?如何配置 TypeScript ( tsconfig.json和Typings.json)?
Edit:Thanks to Aleksey L.and David BohunekI achieved to define a rx-node.d.ts
that looks like this
编辑:感谢Aleksey L.和David Bohunek,我实现了定义一个rx-node.d.ts
看起来像这样的
declare module "rx-node" {
import {Observable} from '@reactivex/rxjs';
import {ReadLine} from "readline";
function fromReadLineStream(stream: ReadLine): Observable<string>
}
I installed @reactivex/rxjs
我安装了@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.jsonto es6
.
我将tsconfig.json 中的目标更改为es6
.
回答by Aleksey L.
Beware, this is old question (2016) regarding managing definitions using typingswhich is deprecated in favor of installing them straight via npm
当心,这是关于管理使用定义的老问题(2016)分型这是有利于通过直安装它们的弃用NPM
You can add custom definitions to typings.json. For example having following folder structure:
您可以将自定义定义添加到Typings.json。例如具有以下文件夹结构:
/typings
/custom
rx-node.d.ts
/global
...
where rx-node.d.ts is your custom typings file. For example:
其中 rx-node.d.ts 是您的自定义类型文件。例如:
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.jsonadd reference to this typings file:
或手动:在typings.json 中添加对此typings文件的引用:
{
"name": "TestName",
"version": false,
"globalDependencies": {
"rx-node": "file:typings/custom/rx-node.d.ts"
}
}
And run typings install
并运行 typings install
回答by David Bohunek
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:
创建一个新文件,命名它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>;
}
This question/answer might be helpuful: How to write d.ts file from existing .js file?
这个问题/答案可能会有所帮助: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
如果您创建了一个不错的、高质量的类型定义文件,请贡献给https://github.com/DefinitelyTyped/DefinitelyTyped