【发布时间】:2017-03-14 08:59:01
【问题描述】:
我已安装 request-promise 库并尝试在我的 TypeScript 应用程序中使用它,但运气不佳。
如果我这样使用它:
import {RequestPromise} from'request-promise';
RequestPromise('http://www.google.com')
.then(function (htmlString) {
// Process html...
})
.catch(function (err) {
// Crawling failed...
});
我在 TS 编译输出中看到了这个:
error TS2304: Cannot find name 'RequestPromise'.
如果我这样使用:
import * as rp from'request-promise';
rp('http://www.google.com')
.then(function (htmlString) {
// Process html...
})
.catch(function (err) {
// Crawling failed...
});
我看到一个错误,指出对象 rp 上没有 '.then()' 方法。
如何正确使用 TypeScript?
【问题讨论】:
-
您是否尝试过
import rp from 'request-promise';,然后像上面一样使用rp? -
现在它可以编译了,但我现在在运行应用程序并调用该代码时看到
TypeError: Uncaught error: request_promise_1.default is not a function。 -
我试过
import rp = require('request-promise'),它修复了错误。谢谢! -
直到我读到this answer,我才明白为什么会这样。 TL;DR -
import * as创建一个模块对象,它不像函数那样“可调用”。
标签: javascript typescript