{"record":{"id":"0b9c1f532b3ddafa","repo":"microsoft/typescript-go","slug":"invalid-cast-the-supplied-value-value-did-not","errorCode":null,"errorMessage":"Invalid cast. The supplied value ${value} did not pass the test '${test.name}'.","messagePattern":"Invalid cast\\. The supplied value (.+?) did not pass the test '(.+?)'\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"_packages/native-preview/src/ast/utils.ts","lineNumber":59,"sourceCode":"\r\n/**\r\n * Add an extra leading underscore to a display name that already begins with\r\n * `__`, producing its escaped {@link __String} key.\r\n */\r\nexport function escapeLeadingUnderscores(identifier: string): __String {\r\n    return (identifier.length >= 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._\r\n        ? \"_\" + identifier\r\n        : identifier) as __String;\r\n}\r\n\r\nexport function tryCast<TOut extends TIn, TIn = any>(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut | undefined {\r\n    return value !== undefined && test(value) ? value : undefined;\r\n}\r\n\r\nexport function cast<TOut extends TIn, TIn = any>(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut {\r\n    if (value !== undefined && test(value)) return value;\r\n\r\n    throw new Error(`Invalid cast. The supplied value ${value} did not pass the test '${test.name}'.`);\r\n}\r\n\r\nexport function hasExpression(node: Node): node is HasExpression {\r\n    return \"expression\" in node;\r\n}\r\n\r\nexport function hasInitializer(node: Node): node is HasInitializer {\r\n    return \"initializer\" in node;\r\n}\r\n\r\nexport function hasObjectAssignmentInitializer(node: Node): node is ObjectAssignmentInitializer {\r\n    return \"objectAssignmentInitializer\" in node;\r\n}\r\n\r\nexport function cloneSourceFileData(sourceFile: SourceFile): Record<string, unknown> {\r\n    return {\r\n        statements: sourceFile.statements,\r\n        endOfFileToken: sourceFile.endOfFileToken,\r","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/microsoft/typescript-go/blob/1bcfa18d79a3be41772223d5c05dfe4480e614ff/_packages/native-preview/src/ast/utils.ts#L41-L77","documentation":"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).","triggerScenarios":"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.","commonSituations":"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).","solutions":["Switch to tryCast(value, test) when the value may legitimately not match, and handle the undefined result","Fix the predicate to match what you're actually narrowing (e.g. isIdentifier vs isIdentifierPart)","Check the node kind before casting: if (node.kind === SyntaxKind.Identifier) ...","Update to the compiler version whose AST matches your assumptions"],"exampleFix":"// before\nconst id = cast(child, isIdentifier);\n\n// after\nconst id = tryCast(child, isIdentifier);\nif (!id) { /* handle non-identifier node */ }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"import { tryCast, isIdentifier } from './ast/utils';\nconst id = tryCast(node, isIdentifier); // undefined instead of throw","tryCatchPattern":"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; }","preventionTips":["Prefer tryCast unless the invariant is truly impossible","Check node.kind before casting in any newly touched traversal path","Add narrowing tests when refactoring node structures"],"tags":["typescript","internal","assertion","ast"],"backgroundTag":null,"analyzedSha":"1bcfa18d79a3be41772223d5c05dfe4480e614ff","analyzedAt":"2026-08-16T02:12:00.115Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}