hunterloftis/throng

Type definitions for v4 backwards compatibility are incorrect

jkepps opened this issue · 0 comments

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.