Skip to content

Instantly share code, notes, and snippets.

@souporserious
Created July 17, 2025 20:07
Show Gist options
  • Save souporserious/81b74ed995402f120284f76843b64217 to your computer and use it in GitHub Desktop.
Save souporserious/81b74ed995402f120284f76843b64217 to your computer and use it in GitHub Desktop.
const callStack: string[] = []
const MAX_DEPTH = 1500
const SNIPPET_LEN = 60
function snippet(text: string) {
return text.replace(/\s+/g, ' ').slice(0, SNIPPET_LEN)
}
/** Traces the type and its enclosing node. */
function traceType(type: Type, node?: Node) {
let nodeInfo = ''
if (node) {
const kindName = tsMorph.SyntaxKind[node.getKind()]
const location = node.getSourceFile()
? node.getStartLineNumber() + ':' + node.getStartLinePos()
: null
nodeInfo = ` • ${kindName}${location ? `@${location}` : ''}${snippet(node.getText())}`
}
callStack.push(
`${snippet(type.getText(undefined, TYPE_FORMAT_FLAGS))}${nodeInfo}`
)
if (callStack.length > MAX_DEPTH) {
throw new Error(
'resolveType recursion depth exceeded > ' +
MAX_DEPTH +
'\n' +
callStack.join('\n ↳ ')
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment