繁体   English   中英

如何在 Typescript 中指定我想要的重载函数?

[英]How to specify which overloaded function I want in Typescript?

我试图承诺标准节点 fs.writeFile。 我有 node 和 bluebird 的类型,这是我在这里选择的 Promise 库:

const f = Promise.promisify(fs.writeFile)
return f(file, content); // Should be a promise

这不起作用,因为:

[ts] 提供的参数与调用目标的任何签名都不匹配。 const f: (arg1: string) => Promise<{}>

所以我选择了错误的重载方法,因为对 promisify 的调用无法真正知道我猜是哪个。 或者甚至不是那个,而是带有可选参数的东西,因为这些是三个重载的 writeFile:

export function writeFile(filename: string, data: any, callback?: (err: NodeJS.ErrnoException) => void): void;
export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: number; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;
export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: string; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void;

promisify 对 # of args 有很多重载,如下所示:

/**
 * Returns a function that will wrap the given `nodeFunction`. Instead of taking a callback, the returned function will return a promise whose fate is decided by the callback behavior of the given node function. The node function should conform to node.js convention of accepting a callback as last argument and calling that callback with error as the first argument and success value on the second argument.
 *
 * If the `nodeFunction` calls its callback with multiple success values, the fulfillment value will be an array of them.
 *
 * If you pass a `receiver`, the `nodeFunction` will be called as a method on the `receiver`.
 */
static promisify<T>(func: (callback: (err:any, result: T) => void) => void, receiver?: any): () => Promise<T>;
static promisify<T, A1>(func: (arg1: A1, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1) => Promise<T>;
static promisify<T, A1, A2>(func: (arg1: A1, arg2: A2, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2) => Promise<T>;
static promisify<T, A1, A2, A3>(func: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3) => Promise<T>;
static promisify<T, A1, A2, A3, A4>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Promise<T>;
static promisify<T, A1, A2, A3, A4, A5>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Promise<T>;
static promisify(nodeFunction: Function, receiver?: any): Function;

因此,似乎正在使用错误的promisify。 有什么更干净的方法可以让我实现我想要的吗? 目前我必须解决这个问题:

const writeFile : (filename: string, data: any, callback: (err: NodeJS.ErrnoException) => void) => void = fs.writeFile
const f = Promise.promisify(writeFile)
return f(file, content); 

我觉得这很丑陋和冗长......

我无法设置一个好的简单的工作示例,但以下类型断言将允许您提示和您期望的内容:

const f = <(file: string, content: string) => void> Promise.promisify(fs.writeFile)

您可以修改<(file: string, content: string) => void>以更好地描述您期望可用的内容,或者使用更广泛的类型<Function>或使用<any>动态化。

第一个选项需要更多的工作,但允许在稍后调用f时推断返回类型(在这种情况下为void - 但您可以想象这种返回类型将被更积极地使用的情况)。

根据您的评论,我猜您需要...

const f = <(file: string, content: string) => Promise<any>> Promise.promisify(fs.writeFile)

你也可以在地方使用的联合类型any在这个例子中,如果你知道了两种可能的结果,即Promise<any>可以是:

Promise<ErrnoException | MySuccessfulType>

这将允许返回类型为Promise<ErrnoException>或返回类型为Promise<MySuccessfulType>

你可以使用__promisify__fs.writeFile命名空间:

fs.writeFile.__promisify__(file, content)

否则,您必须明确提及泛型类型以选择相应的重载:

  • Node内置的util包有一个promisify()函数
const f = util.promisify<PathLike, string, void>(fs.writeFile)
f(file, content)

暂无
暂无

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

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