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

0lvr5msh  于 2023-10-17  发布在  Node.js
关注(0)|答案(3)|浏览(95)

我正在尝试promisify标准节点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;

因此,它似乎是错误的承诺,而正在使用。有没有更干净的方法让我达到我想要的?目前我必须解决这个问题:

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

我觉得这很难看也很冗长

yh2wf1be

yh2wf1be1#

我还没有能够建立一个好的简单的工作示例,但下面的类型Assert将允许您提示和您所期望的:

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)

在这个例子中,如果你知道两种可能的结果,你也可以使用一个联合类型来代替anyPromise<any>可以是:

Promise<ErrnoException | MySuccessfulType>

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

8cdiaqws

8cdiaqws2#

你可以从fs.writeFile命名空间使用__promisify__

fs.writeFile.__promisify__(file, content)

否则,你必须显式地提到泛型类型来选择相应的重载:

  • Node内置util包,具有promisify()功能
const f = util.promisify<PathLike, string, void>(fs.writeFile)
f(file, content)
mccptt67

mccptt673#

我最终做的是指定自己的重载类型,这个方法的主要缺点是,一旦库更新,我不会得到通知。
mixpanel的例子:

const mixpanelClient = Mixpanel.init(mixpanelProjectApiToken, {
    protocol: 'https',
});

type trackFunc = (eventName: string, properties: PropertyDict) => Promise<void>;

const track: trackFunc = promisify(mixpanelClient.track.bind(mixpanelClient));

相关问题