typescript augment "rx" module => 无效的模块名称
typescript augment "rx" module => Invalid module name
我正在编写一个也包含类型的节点模块,因为我将它用于打字稿项目。该模块依赖于 "rx": "^4.0.6"
。此版本的 rx
包含 rx/ts
中的类型。我想要做的是使用 2 个自定义函数来扩充 rx
类型。
模块结构
myModule
|-- ts
|-- index.ts
|-- package.json
|-- tsconfig.json
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"declaration": true,
"rootDir": "ts",
"outDir": "./dist"
}
}
package.json
{
"name": "myModule",
"version": "1.2.0",
"private": true,
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"preinstall": "npm run build",
"build": "../../node_modules/typescript/bin/tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@types/express": "^4.0.35",
"express": "^4.0.35",
"rx": "^4.0.6"
},
"devDependencies": {
"typescript": "~2.3.4"
}
}
ts/index.ts 看起来像下面这样(不包括所有提到的函数的实现):
import * as Rx from 'rx';
import * as express from 'express';
declare module "rx" {
interface Observable<T> {
pipeValueExpressResponse: (res: express.Response) => void;
pipeArrayExpressResponse: (res: express.Response) => void;
}
}
// export some types...
Rx.Observable.prototype.pipeValueExpressResponse = function(res:
express.Response) {
sendValue(this, res);
};
Rx.Observable.prototype.pipeArrayExpressResponse = function(res:
express.Response) {
foldResponse(this, res);
};
// export some functions...
编译模块导致Invalid module name in augmentation. Module 'rx' resolves to an untyped module at '.../myModule/node_modules/rx/index.js', which cannot be augmented.
我已经阅读了很多关于打字稿中模块扩充的答案,但无法解决我的问题。
编辑
对 rx/ts
中包含的 rx
类型使用 /// <reference path="" />
解决了 Invalid module name
问题,但现在我收到 Property 'prototype' does not exist on type 'ObservableStatic'
错误
您应该安装 rx
模块的类型:
npm install @types/rx
通过向 index.ts
中的 rx/ts/rx.all.d.ts
类型添加 /// <reference path="" />
设法解决了 Invalid module name
问题。
之后我得到了一个 Property 'prototype' does not exist on type 'ObservableStatic'
错误,我通过添加以下内容修复了该错误:
declare module "rx" {
interface ObservableStatic {
prototype: any;
}
interface Observable<T> {
pipeValueExpressResponse: (res: express.Response) => void;
pipeArrayExpressResponse: (res: express.Response) => void;
}
}
我正在编写一个也包含类型的节点模块,因为我将它用于打字稿项目。该模块依赖于 "rx": "^4.0.6"
。此版本的 rx
包含 rx/ts
中的类型。我想要做的是使用 2 个自定义函数来扩充 rx
类型。
模块结构
myModule
|-- ts
|-- index.ts
|-- package.json
|-- tsconfig.json
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"declaration": true,
"rootDir": "ts",
"outDir": "./dist"
}
}
package.json
{
"name": "myModule",
"version": "1.2.0",
"private": true,
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"preinstall": "npm run build",
"build": "../../node_modules/typescript/bin/tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@types/express": "^4.0.35",
"express": "^4.0.35",
"rx": "^4.0.6"
},
"devDependencies": {
"typescript": "~2.3.4"
}
}
ts/index.ts 看起来像下面这样(不包括所有提到的函数的实现):
import * as Rx from 'rx';
import * as express from 'express';
declare module "rx" {
interface Observable<T> {
pipeValueExpressResponse: (res: express.Response) => void;
pipeArrayExpressResponse: (res: express.Response) => void;
}
}
// export some types...
Rx.Observable.prototype.pipeValueExpressResponse = function(res:
express.Response) {
sendValue(this, res);
};
Rx.Observable.prototype.pipeArrayExpressResponse = function(res:
express.Response) {
foldResponse(this, res);
};
// export some functions...
编译模块导致Invalid module name in augmentation. Module 'rx' resolves to an untyped module at '.../myModule/node_modules/rx/index.js', which cannot be augmented.
我已经阅读了很多关于打字稿中模块扩充的答案,但无法解决我的问题。
编辑
对 rx/ts
中包含的 rx
类型使用 /// <reference path="" />
解决了 Invalid module name
问题,但现在我收到 Property 'prototype' does not exist on type 'ObservableStatic'
错误
您应该安装 rx
模块的类型:
npm install @types/rx
通过向 index.ts
中的 rx/ts/rx.all.d.ts
类型添加 /// <reference path="" />
设法解决了 Invalid module name
问题。
之后我得到了一个 Property 'prototype' does not exist on type 'ObservableStatic'
错误,我通过添加以下内容修复了该错误:
declare module "rx" {
interface ObservableStatic {
prototype: any;
}
interface Observable<T> {
pipeValueExpressResponse: (res: express.Response) => void;
pipeArrayExpressResponse: (res: express.Response) => void;
}
}