简体   繁体   English

如何从 typescript 中的库中提取接口?

[英]How do I extract an interface from a library in typescript?

I a using the library puppeteer however I would only like to instantiate one browser.我使用库 puppeteer 但是我只想实例化一个浏览器。 So I'm using a top level function to create the browser and passing it as a parameter to helper functions like so:所以我使用顶级 function 来创建浏览器并将其作为参数传递给辅助函数,如下所示:

import * as puppeteer from 'puppeteer';
export async function scrape() {
    const browser = await puppeteer
        .launch({
            //product:'chrome',
            //executablePath: '/usr/bin/chromium-browser,
            args: ['--no-sandbox', '--disable-setuid-sandbox'],
        })
        .catch(() => {});
    scrapeVmb(browser)
    scrapeChase(browser)
}

The problem becomes that I lose the intelisense inside the helper functions for the pupeteer library, This problem could be solved by setting the type of the browser parameter, however I do not know where to find the type of browser.问题变成了我在 pupeteer 库的辅助函数中丢失了智能感知,这个问题可以通过设置浏览器参数的类型来解决,但是我不知道在哪里可以找到浏览器的类型。

TLDR TLDR
How can I get the browser parameter to inherit the browser type inside the "scrapeVmb" and "scrapeChase" functions.如何获取浏览器参数以继承“scrapeVmb”和“scrapeChase”函数中的浏览器类型。

After fiddling around with it some more (I don't understand HOW this works so if someone knows it might be worthy of an answer) you can do:在再摆弄它之后(我不明白这是如何工作的,所以如果有人知道它可能值得回答)你可以这样做:

type browserType = puppeteer.Browser

That will give you the browser interface which you can then use like so:这将为您提供浏览器界面,然后您可以像这样使用它:

function scrapeEtc(browser:browserType) {
  //...
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM