Skip to content

exec generic type def doubly wrapped Promise? #485

Open
@vincerubinetti

Description

Currently the type definition for exec looks like this:

exec<T extends (...args: any[]) => any>(method: string | T, params?: Parameters<T> | null | undefined, options?: import("./types.js").ExecOptions | undefined): Promise<ReturnType<T>>;

Note the Promise<ReturnType<T>>. I think this unnecessarily wraps the ReturnType in a Promise when the return type already is a promise. I think it should be something like Promisify<ReturnType<T>> and type Promisify<T> = T extends Promise<any> ? T : Promise<T>; (wraps type in promise, but only if not already a promise).

I pretty sure it should be like this, because just testing out...

// worker

export const encode = async (blah: any) => {
  const encoder = await createEncoder();
  // ...
  return new Blob();
};

export type Encode = typeof encode;

workerpool.worker({ encode });

// consumer

import EncodeWorker from "./encode.worker?worker&url";
import { Encode } from "./encode.worker.ts";

const encoderPool = workerpool.pool(EncodeWorker);
const getBlob = (blah: any) =>
  encoderPool.exec<Encode>("encode", [blah]);

// test

getBlob(blah).then((result) => console.log(result));

Typescript says result is type Promise<Blob>, but console log shows just Blob. I didn't look at the exec implementation, but the result is definitely just a blob, so the typing must be wrong.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions