Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a module in typescript (array syntax)?

import http from "http";
import https from "https";

const module = (options.port === 443 ? "https" : "http");

const req = [module].request(options, (res) => {
  console.log(res.statusCode);
});

error TS2339: Property ‘request’ does not exist on type ‘string[]’.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The [module] array syntax should be changed to module.

Additionally, you should use conditional importing to only import the module that you require. Please note that because import returns a promise, you will need to add the code in your example into an async function and prepend await in front of each import statement:

async function invokeHttpRequest() {
  const module = (options.port === 443 ? await import("https") : await import("http"));

  const req = module.request(options, (res) => {
    console.log(res.statusCode);
  });
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading