Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type definitions for v4 backwards compatibility are incorrect #46

Open
jkepps opened this issue Nov 4, 2021 · 0 comments
Open

Type definitions for v4 backwards compatibility are incorrect #46

jkepps opened this issue Nov 4, 2021 · 0 comments

Comments

@jkepps
Copy link

jkepps commented Nov 4, 2021

The @types/throng type definitions say that the first argument of throng() can be a number or a string if using the v4 definition, but in the source code parsedOptions() only checks if it is a function or a number, not a string.

function parseOptions(options = {}, startFunction) {
  if (typeof options === 'function') {
    return { worker: options }
  }
  if (typeof options === 'number') {
    return { count: options, worker: startFunction }
  }
  return {
    master: options.master,
    worker: options.worker || options.start,
    count: options.count !== undefined ? options.count : options.workers,
    lifetime: options.lifetime,
    grace: options.grace,
    signals: options.signals,
  }
}

If a string is passed in for options as is allowed by the type defs, the throng invocation will error out.

the type defs:

declare function throng(startOrOptions: throng.WorkerCallback | throng.Options): Promise<void>;
declare function throng(workers: throng.WorkerCount, start: throng.WorkerCallback): Promise<void>;
declare namespace throng {
    type WorkerCount = number | string;
    type WorkerCallback = (id: number, disconnect: () => void) => void;
    type MasterCallback = () => void;

    type Options = {
        signals?: string[] | undefined;
        grace?: number | undefined;
        lifetime?: number | undefined;
        master?: MasterCallback | undefined;
        count?: number | undefined;
        workers?: WorkerCount | undefined;
    } & ({start: WorkerCallback} | {worker: WorkerCallback});
}

Either the source code should be updated to account for options being a string, or the type defs should be updated to enforce a number is passed if using the legacy API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant