apple/pkl · error · VmException

invalidTypeName

invalidTypeName

Error message

invalidTypeName

What it means

Thrown when the text of a type-reference context does not match any recognized type-name shape. visitType(GrAdditiveTypeContext etc.) switches on the grammar alternative; the `default` arm rejects anything that is not a valid type expression form (identifier, qualified name, module type, etc.).

Source

Thrown at pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java:2404

        yield new ResolveSimpleDeclaredTypeNode(
            sourceSection,
            org.pkl.core.runtime.Identifier.get(identifier.getValue()),
            isBaseModule,
            getModuleNode);
      }
      case 2 -> {
        var identifier1 = identifiers.get(0);
        var identifier2 = identifiers.get(1);
        yield new ResolveQualifiedDeclaredTypeNode(
            createSourceSection(ctx),
            createSourceSection(identifier1),
            createSourceSection(identifier2),
            org.pkl.core.runtime.Identifier.localProperty(identifier1.getValue()),
            org.pkl.core.runtime.Identifier.get(identifier2.getValue()),
            getModuleNode);
      }
      default ->
          throw exceptionBuilder()
              .evalError("invalidTypeName", ctx.text())
              .withSourceSection(createSourceSection(ctx))
              .build();
    };
  }

  private ExpressionNode doVisitObjectBody(
      List<? extends ObjectBody> bodies, ExpressionNode parentNode) {
    for (var ctx : bodies) {
      parentNode = doVisitObjectBody(ctx, parentNode);
    }
    return parentNode;
  }

  private void addObjectNamesToScope(ObjectScope scope, ObjectBody body) {
    for (var member : body.getMembers()) {
      if (member instanceof ObjectProperty prop) {
        var local = hasLocalModifier(prop.getModifiers());

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Inspect `ctx.text()` in the message and rewrite the type using valid Pkl type syntax: `String`, `Listing<Int>`, `module`, `unknown`, unions `A|B`, conjunctions `A&B`.
  2. Remove stray punctuation/operators from the type position.
  3. Wrap the intended type reference in parentheses correctly if composing unions/conjunctions.

Example fix

// before
name: String.?
// after
name: String?
Defensive patterns

Strategy: validation

Validate before calling

// allow only known type-position constructs before handing to Pkl
const VALID = /^[A-Za-z_][\w.]*(\s*[|&]\s*[A-Za-z_][\w.]*|<[^>]+>|\?)*$/;
if (!VALID.test(typeText)) throw new Error(`malformed type: ${typeText}`);

Prevention

When it happens

Trigger: Using an invalid token or malformed expression where a type is expected, e.g. `x: (a | b).c`, an operator in a type position, or a malformed union/intersection with unexpected structure. The error reports `ctx.text()` of the offending region.

Common situations: Typos in type positions (`x: Strng` is actually a valid identifier that fails resolution, but stray punctuation like `x: String.?` hits this); editing types and leaving dangling operators; copy-paste of type syntax from other languages (e.g. TypeScript's `typeof`).

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/205b4a763afc5498. Report an issue: GitHub.