How to use RequestPromise in TypeScript?

I installed the request-promise library and tried to use it in my TypeScript application, but without much luck.

If I use it as follows:

import {RequestPromise} from'request-promise'; RequestPromise('http://www.google.com') .then(function (htmlString) { // Process html... }) .catch(function (err) { // Crawling failed... }); 

I see this at the output of TS compilation:

 error TS2304: Cannot find name 'RequestPromise'. 

If I use it as follows:

 import * as rp from'request-promise'; rp('http://www.google.com') .then(function (htmlString) { // Process html... }) .catch(function (err) { // Crawling failed... }); 

I see an error that says the .then () method is missing on the rp object.

How to use it correctly with TypeScript?

+5
source share
1 answer

You must import all ( * ) not only RequestPromise :

 import * as request from "request-promise"; request.get(...); 

This answer details the differences between import/from and require .

+3
source

Source: https://habr.com/ru/post/1259082/


All Articles