typescript - 如何使用代理方法扩展 super 代理?

标签 typescript superagent

我还没有找到 superagent-proxy 的任何 TypeScript 定义。因此,当我尝试将我的 TypeScript 应用程序编译为 JavaScript 时,我收到错误消息:

ts: Property 'proxy' does not exist on type 'SuperAgentRequest'.

import * as request from 'superagent';
import * as withProxy from 'superagent-proxy';

withProxy(request);

request
    .get(...)
    .proxy(proxy)

我已经创建了一个包含声明的文件,但我不知道还能做什么。

declare module 'superagent-proxy';

我想我想定义一些高阶函数,它接受一个 super 代理并以某种方式返回一个带有代理的 super 代理。

这是我迄今为止最好的尝试:

import * as request from 'superagent';

declare module 'superagent-proxy' {
    interface SuperAgentRequestWithProxy extends request.SuperAgentStatic {
        proxy(url: string): SuperAgentRequestWithProxy;
    }
}

但它不起作用,我什至不确定它是否接近正确。

Invalid module name in augmentation. Module 'superagent-proxy' resolves to an untyped module at '.../node_modules/superagent-proxy/index.js', which cannot be augmented

最佳答案

你还需要添加类型依赖,只导入 super agent 代理依赖是不够的,你需要有两个导入: "superagent-proxy": "^2.1.0", under dependencies
"@types/superagent-proxy": "^2.0.0", under dev dependencies

关于typescript - 如何使用代理方法扩展 super 代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57721229/

相关文章:

typescript - 在 Jest 中使用带有 Typescript 的 node-fetch 时出现错误 "SyntaxError: Cannot use import statement outside a module"

node.js - 使用 SailsJS/Node、Mocha、Supertest 进行身份验证的 session 存储

node.js - Superagent 在异步 waterfall 中移动响应回调位置

angular - 范围错误 : Maximum call stack size exceeded Lazy routing Angular 2

javascript - 错误typeerror : _co. onclk不是一个函数

javascript - 像Superagent这样的语法,我该怎么做?

reactjs - promise 错误: Objects are not valid as a React child

javascript - 使用 nock 模拟具有特定请求 header 的 mocha 中的 super 代理请求

javascript - 拒绝 http.get catch block 中的 promise

node.js - Sequelize - 无法读取未定义的属性 'list'