angular/angular-cli · error · TypeError

Target name must be a string.

Error message

Target name must be a string.

What it means

TargetDefinitionCollection._validateName only checks that a target's name is a string; unlike project names there is no pattern restriction. If name is not a string (undefined, number, object), a TypeError is thrown before the target is created.

Source

Thrown at packages/angular_devkit/core/src/workspace/definitions.ts:248

      defaultConfiguration: definition.defaultConfiguration,
    };

    super.set(definition.name, target);

    return target;
  }

  override set(name: string, value: TargetDefinition): this {
    this._validateName(name);

    super.set(name, value);

    return this;
  }

  private _validateName(name: string): void {
    if (typeof name !== 'string') {
      throw new TypeError('Target name must be a string.');
    }
  }
}

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Ensure the target name is a non-empty string before calling addTarget/set.
  2. Default or compute the name when it comes from user config (e.g. name ?? 'build').
  3. Add a typeof name === 'string' check in generating code that passes dynamic names.

Example fix

// before
project.addTarget({ name: cfg.targetName, builder: '...' }); // cfg.targetName may be undefined
// after
project.addTarget({ name: String(cfg.targetName ?? 'build'), builder: '...' });
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof targetName !== 'string') {
  throw new Error(`Target name must be a string, got ${typeof targetName}`);
}
project.addTarget({ name: targetName, builder });

Type guard

function isTargetName(v: unknown): v is string {
  return typeof v === 'string' && v.length > 0;
}

Try / catch

try {
  project.addTarget({ name, builder });
} catch (e) {
  if (e instanceof TypeError && e.message === 'Target name must be a string.') {
    project.addTarget({ name: String(name), builder });
  } else throw e;
}

Prevention

When it happens

Trigger: addTarget({ name: undefined, ... }) from a destructured/missing variable; calling set() with a numeric or object key coerced through the API; passing name from untyped JS code.

Common situations: JavaScript (untyped) tooling building targets dynamically; missing object property in a config-driven generator; typos like { Name: 'build' } leaving name undefined.

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/1413feb3913a5761. Report an issue: GitHub.