简体   繁体   English

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

[英]TypeScript: what to do when return type of library function is unknown

I am new to TypeScript and I don't know what type to use when I am calling library functions / methods. 我是TypeScript的新手,我不知道在调用库函数/方法时要使用什么类型。 For example I am using the headless chrome module in my Node.js project. 例如,我在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;
}

Should I use any or leave it without type? 我应该使用任何一种或不使用类型吗?

Assuming you cannot find typings for your library, at the very least I would have the function return a promise of something, since you know it is async. 假设你找不到你的库的类型,至少我会让函数返回一个东西的承诺,因为你知道它是异步的。

Newer versions of TypeScript introduced the unknown type: 较新版本的TypeScript引入了unknown类型:

async function launchBrowser (): Promise<unknown>

But you can also return a promise of any : 但你也可以回复any承诺:

async function launchBrowser (): Promise<any>

Check out the docs for the new unknown type: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html 查看新unknown类型的文档: https//www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html

Before using unknown or any , I would try seeing if the types for your library exist in npm @ types to see if the type information can be located there. 在使用unknownany之前,我会尝试查看您的库的类型是否存在于npm @ types以查看类型信息是否可以位于那里。

It looks like there are types for puppeteer: 看起来有木偶的类型:

npm install --save @types/puppeteer

As far as what you should do, that's more up to you. 至于你应该做什么,这更取决于你。 If hypothetically no types were available, then you could be kind of stuck. 如果假设没有可用的类型,那么你可能会陷入困境。 You could create your own typings file and merge it via Typescript's Declaration Merging feature. 您可以创建自己的打字文件,并通过Typescript的声明合并功能将其合并。 However, I would advise against that because 但是,我会建议反对,因为

A. You'd have to update your typings file everytime you updated the library, which is a pain 答:每次更新库时都需要更新打包文件,这很痛苦

B. You could end up really confusing yourself if you aren't completely correct about the types that the library returns B.如果你对图书馆返回的类型不完全正确,你最终可能会感到困惑

I would probably just leave it at unknown or any to make things easier. 我可能会把它留在unknownany使事情变得更容易。

Edit: The better answer is to follow @Frank Modica's answer and wrap the result in a Promise, such as Promise<any> or Promise<unknown> . 编辑:更好的答案是遵循@Frank Modica的答案并将结果包装在Promise中,例如Promise<any>Promise<unknown> This provides at least some intellisense info and is much better than simply defining it as any or unknown as I had previously stated. 这提供了至少一些智能感知信息,并且比简单地将其定义为我之前所述的anyunknown要好得多。

暂无
暂无

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

相关问题 什么时候需要在TypeScript中声明返回函数类型? - When is it necessary to declare return function type in TypeScript? 在打字稿中,返回空函数的函数的返回类型是什么? - In typescript, what is the return type of a function that returns a void function? 如何在 TypeScript 中覆盖标准库 function 的类型? - How do I override type of a standard library function in TypeScript? 使用:和=&gt;对于带有TypeScript函数的返回类型有什么区别? - What is the difference between using : and => for the return type with a TypeScript function? 返回类型1,-1和0在排序函数中有什么作用? - What does the return type 1, -1 and 0 do in a sort function? 我一直发现自己在使用 TypeScript 时遇到了同样的错误。 当我需要返回一个执行后已知的类型时,我该怎么办? - I keep finding myself in the same error with TypeScript. What do I do when I need to return a type that will be known after execution? TypeScript + Monaco Editor 中的未知类型是什么? - What is Unknown Type in TypeScript + Monaco Editor? 我是否需要在 javascript/typescript 中为匿名函数指定返回类型? - Do I need to specify a return type for an anonymous function in javascript / typescript? 当期望返回与输入相同的类型时,如何验证 TypeScript 的 function - How to validate function for TypeScript when expecting same type for the return as for the input 打字稿中的覆盖函数返回类型 - Override function return type in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM