TypeScript:如何为已安装的 npm 包定义自定义类型?
TypeScript: How to define custom typings for installed npm package?
我喜欢在 TypeScript
中使用 rx-node
import RxNode from 'rx-node';
我使用 npm
安装了 rx-node
$ npm install rx-node --save
我搜索了类型定义,但没有任何结果
$ typings search rx-node
No results found for search
如何为已安装的 npm 模块定义自定义类型定义 rx-node?我应该在哪里存储类型定义文件?如何配置 TypeScript (tsconfig.json 和 typings.json)?
编辑: 感谢 and 我定义了一个 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
。
创建一个新文件,将其命名为 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>;
}
这个question/answer可能会有帮助:How to write d.ts file from existing .js file?
如果您创建了一个漂亮的高质量类型定义文件,请为 https://github.com/DefinitelyTyped/DefinitelyTyped
做出贡献
注意,这是关于使用 typings which is deprecated in favor of installing them straight via npm
管理定义的老问题 (2016)
您可以将自定义定义添加到 typings.json。例如具有以下文件夹结构:
/typings
/custom
rx-node.d.ts
/global
...
其中 rx-node.d.ts 是您的自定义类型文件。例如:
declare module RxNode {
export interface ...
}
然后用命令安装
typings install file:typings/custom/rx-node.d.ts --save --global
或手动:在 typings.json 中添加对此类型文件的引用:
{
"name": "TestName",
"version": false,
"globalDependencies": {
"rx-node": "file:typings/custom/rx-node.d.ts"
}
}
和运行typings install
对于使用 create-react-app (react-scripts) 的 ReactJS 项目,您可以在 src/react-app-env.d.ts
中为您的非类型化包添加模块声明。
declare module "not-typed";
注意:此文件的位置将来可能会更改,请参阅 this issue。
我喜欢在 TypeScript
中使用 rx-nodeimport RxNode from 'rx-node';
我使用 npm
安装了 rx-node$ npm install rx-node --save
我搜索了类型定义,但没有任何结果
$ typings search rx-node
No results found for search
如何为已安装的 npm 模块定义自定义类型定义 rx-node?我应该在哪里存储类型定义文件?如何配置 TypeScript (tsconfig.json 和 typings.json)?
编辑: 感谢 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
。
创建一个新文件,将其命名为 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>;
}
这个question/answer可能会有帮助:How to write d.ts file from existing .js file?
如果您创建了一个漂亮的高质量类型定义文件,请为 https://github.com/DefinitelyTyped/DefinitelyTyped
做出贡献注意,这是关于使用 typings which is deprecated in favor of installing them straight via npm
管理定义的老问题 (2016)您可以将自定义定义添加到 typings.json。例如具有以下文件夹结构:
/typings
/custom
rx-node.d.ts
/global
...
其中 rx-node.d.ts 是您的自定义类型文件。例如:
declare module RxNode {
export interface ...
}
然后用命令安装
typings install file:typings/custom/rx-node.d.ts --save --global
或手动:在 typings.json 中添加对此类型文件的引用:
{
"name": "TestName",
"version": false,
"globalDependencies": {
"rx-node": "file:typings/custom/rx-node.d.ts"
}
}
和运行typings install
对于使用 create-react-app (react-scripts) 的 ReactJS 项目,您可以在 src/react-app-env.d.ts
中为您的非类型化包添加模块声明。
declare module "not-typed";
注意:此文件的位置将来可能会更改,请参阅 this issue。