microsoft/typescript-go · error · Error

Invalid cast. The supplied value ${value} did not pass the t

Error message

Invalid cast. The supplied value ${value} did not pass the test '${test.name}'.

What it means

The assert-style cast helper in the compiler's AST utilities: cast(value, isX) requires value to pass the given type predicate. This is an internal invariant assertion — hitting it means the code called cast with a test the value genuinely fails (or value was undefined).

Source

Thrown at _packages/native-preview/src/ast/utils.ts:59

/**
 * Add an extra leading underscore to a display name that already begins with
 * `__`, producing its escaped {@link __String} key.
 */
export function escapeLeadingUnderscores(identifier: string): __String {
    return (identifier.length >= 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._
        ? "_" + identifier
        : identifier) as __String;
}

export function tryCast<TOut extends TIn, TIn = any>(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut | undefined {
    return value !== undefined && test(value) ? value : undefined;
}

export function cast<TOut extends TIn, TIn = any>(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut {
    if (value !== undefined && test(value)) return value;

    throw new Error(`Invalid cast. The supplied value ${value} did not pass the test '${test.name}'.`);
}

export function hasExpression(node: Node): node is HasExpression {
    return "expression" in node;
}

export function hasInitializer(node: Node): node is HasInitializer {
    return "initializer" in node;
}

export function hasObjectAssignmentInitializer(node: Node): node is ObjectAssignmentInitializer {
    return "objectAssignmentInitializer" in node;
}

export function cloneSourceFileData(sourceFile: SourceFile): Record<string, unknown> {
    return {
        statements: sourceFile.statements,
        endOfFileToken: sourceFile.endOfFileToken,

View on GitHub (pinned to 1bcfa18d79)

Solutions

  1. Switch to tryCast(value, test) when the value may legitimately not match, and handle the undefined result
  2. Fix the predicate to match what you're actually narrowing (e.g. isIdentifier vs isIdentifierPart)
  3. Check the node kind before casting: if (node.kind === SyntaxKind.Identifier) ...
  4. Update to the compiler version whose AST matches your assumptions

Example fix

// before
const id = cast(child, isIdentifier);

// after
const id = tryCast(child, isIdentifier);
if (!id) { /* handle non-identifier node */ }
Defensive patterns

Strategy: type-guard

Type guard

import { tryCast, isIdentifier } from './ast/utils';
const id = tryCast(node, isIdentifier); // undefined instead of throw

Try / catch

try { const id = cast(node, isIdentifier); } catch (e) { if (e.message.startsWith('Invalid cast.')) { /* wrong predicate or wrong node: log node.kind and fix caller */ } throw e; }

Prevention

When it happens

Trigger: Calling cast(node, isIdentifier) on a node that is not an Identifier; reusing a predicate written for a different node kind; passing undefined where the caller assumed a node exists.

Common situations: Patches to compiler code or tooling built on internal APIs where a node's kind was assumed (e.g. after a refactor changed what a parent node can contain).

Related errors


AI-assisted analysis of microsoft/typescript-go@1bcfa18d79 (2026-08-16). Data as JSON: /api/errors/0b9c1f532b3ddafa. Report an issue: GitHub.