angular/angular-cli · error

Expected an object options as second argument.

Error message

Expected an object options as second argument.

What it means

When the first argument to SimpleJobRegistry.register() is a JobHandler, the second argument (if supplied) must be the options object. This TypeError is thrown when a handler is passed first but the second argument is not a plain object — typically the name string was passed in the wrong position.

Source

Thrown at packages/angular_devkit/architect/src/jobs/simple-registry.ts:83

  ): void;

  register<ArgumentT extends JsonValue, InputT extends JsonValue, OutputT extends JsonValue>(
    nameOrHandler: JobName | JobHandler<ArgumentT, InputT, OutputT>,
    handlerOrOptions: JobHandler<ArgumentT, InputT, OutputT> | RegisterJobOptions = {},
    options: RegisterJobOptions = {},
  ): void {
    // Switch on the arguments.
    if (typeof nameOrHandler == 'string') {
      if (!isJobHandler(handlerOrOptions)) {
        // This is an error.
        throw new TypeError('Expected a JobHandler as second argument.');
      }

      this._register(nameOrHandler, handlerOrOptions, options);
    } else if (isJobHandler(nameOrHandler)) {
      if (typeof handlerOrOptions !== 'object') {
        // This is an error.
        throw new TypeError('Expected an object options as second argument.');
      }

      const name = options.name || nameOrHandler.jobDescription.name || handlerOrOptions.name;
      if (name === undefined) {
        throw new TypeError('Expected name to be a string.');
      }

      this._register(name, nameOrHandler, options);
    } else {
      throw new TypeError('Unrecognized arguments.');
    }
  }

  protected _register<
    ArgumentT extends JsonValue,
    InputT extends JsonValue,
    OutputT extends JsonValue,
  >(

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Pass the name inside the options object: registry.register(handler, { name: 'some-name' }).
  2. Ensure the name is set on the handler itself via createJobHandler(..., { name: 'some-name' }) and call register(handler) with no second argument.
  3. If you intended the name-first form, reorder arguments: registry.register('some-name', handler, options).

Example fix

// before
registry.register(myHandler, 'my-job');
// after
registry.register(myHandler, { name: 'my-job' });
Defensive patterns

Strategy: validation

Validate before calling

if (typeof secondArg !== 'undefined' && (secondArg === null || typeof secondArg !== 'object' || Array.isArray(secondArg))) {
  throw new Error('second argument to register(handler, ...) must be an options object');
}

Type guard

function isRegisterOptions(v) {
  return v === undefined || (v !== null && typeof v === 'object' && !Array.isArray(v));
}

Try / catch

try {
  registry.register(handler, opts);
} catch (e) {
  if (e instanceof TypeError && /object options as second argument/.test(e.message)) {
    throw new Error(`bad second argument type: ${typeof opts} — pass { name: '...' } instead`);
  }
  throw e;
}

Prevention

When it happens

Trigger: register(handler, 'some-name') — passing a string name as the second argument instead of an options object like { name: 'some-name' }; also triggered by passing a function, number, or other non-object as the second argument alongside a handler.

Common situations: Developers expecting a register(name, handler) overload but passing (handler, name); older code written against a different registry API; confusion after switching between register(handler) and register(name, handler) forms.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/9b7376ecf9ad9714. Report an issue: GitHub.