[英]How do I use old typescript typings after npm package was removed and renamed?
[英]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 )?
编辑:感谢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>
}
我安装了@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
。
当心,这是关于管理使用定义的老问题(2016)分型这是有利于通过直安装它们的弃用NPM
您可以将自定义定义添加到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 中添加对此typings文件的引用:
{
"name": "TestName",
"version": false,
"globalDependencies": {
"rx-node": "file:typings/custom/rx-node.d.ts"
}
}
并运行typings install
创建一个新文件,命名为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>;
}
这个问题/答案可能会有所帮助: How to write d.ts file from existing .js file?
如果您创建了一个不错的、高质量的类型定义文件,请贡献给https://github.com/DefinitelyTyped/DefinitelyTyped
对于使用 create-react-app (react-scripts) 的 ReactJS 项目,您可以在src/react-app-env.d.ts
为您的无类型包添加模块声明。
declare module "not-typed";
注意:以后此文件的位置可能会更改,请参阅此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.