TypeScript:库函数的返回类型未知时该怎么办

Vasile AlexandruPeşte

我是TypeScript的新手,我不知道在调用库函数/方法时使用哪种类型。例如,我在Node.js项目中使用无头chrome模块。

import puppeteer = require("puppeteer");

async function launchBrowser () {
    const browser = await puppeteer.launch();

    return browser;
}

// In this case I do not know the return type of the launch method. What should I do?

async function launchBrowser (): Promise<any> {
    const browser: any = await puppeteer.launch();

    return browser;
}

我应该使用任何类型还是将其保留为无类型?

弗兰克·莫迪卡

假设您找不到您的库的类型,至少我希望该函数返回某个承诺,因为您知道它是异步的。

较新版本的TypeScript引入了以下unknown类型:

async function launchBrowser (): Promise<unknown>

但您也可以返回以下承诺any

async function launchBrowser (): Promise<any>

签出新unknown类型的文档https : //www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

库函数参数不是const时该怎么办

类型包含自身时该怎么办?

当snapshot.exists()返回false时该怎么办?

当函数调用动态创建的元素时该怎么办?

Kotlin:返回泛型类型的函数。如果我不知道应该返回哪种具体类型该怎么办?

队列关闭时该怎么办?

UPS过载时该怎么办?

桌面冻结时该怎么办?

当列是数组时,该怎么办?

当ember-cli rimraf node_module需要glob并返回“未定义的不是函数”时,该怎么办?

当MPI中发送消息的数量未知时,我该怎么办?

消耗REST服务,方法返回多种类型..该怎么办

.where()无法将数据转换回原始类型时该怎么办

当我不知道需要的数据类型时该怎么办?

如果我忽略了shared_ptr返回类型的函数的返回值怎么办

VBA中的自动筛选不返回数据时该怎么办?

当我在Python或IPython控制台中时,返回输出该怎么办?

RESTful API:无返回值时该怎么办

当我使用的TypeScript定义文件缺少单个定义时,该怎么办?

我该怎么办呢?该怎么办?

如果销毁选项类型内的对象该怎么办?

Ruby 中的静态类型推断,我该怎么办?

该消息怎么办?

如果isAvailableForServiceType方法返回NO,该怎么办

Mikrotik:execute命令返回什么,该怎么办?

findViewById()返回null-我该怎么办?

写入返回较小的尺寸该怎么办?

我的ajax调用总是返回错误,该怎么办

收到Alamofire的返回数据后,我该怎么办?