refactor: improve type safety patterns from @ts-expect-error cleanup

Amp-Thread-ID: https://ampcode.com/threads/T-019bb3bd-f607-735a-b1a8-fce5fe4f0125
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
DrJKL
2026-01-12 12:14:10 -08:00
parent 4ef1fd984b
commit 7c063676be
11 changed files with 237 additions and 148 deletions

View File

@@ -0,0 +1,58 @@
/**
* Represents a subgraph's graph object with inputs and outputs slots.
*/
export interface SubgraphGraph {
inputs: SubgraphSlot[]
outputs: SubgraphSlot[]
}
export interface SubgraphSlot {
label?: string
name?: string
displayName?: string
}
/**
* Type guard to check if a graph object is a subgraph.
*/
export function isSubgraph(graph: unknown): graph is SubgraphGraph {
return (
graph !== null &&
typeof graph === 'object' &&
'inputs' in graph &&
'outputs' in graph &&
Array.isArray((graph as SubgraphGraph).inputs) &&
Array.isArray((graph as SubgraphGraph).outputs)
)
}
/**
* Assertion function that throws if the graph is not a subgraph.
*/
export function assertSubgraph(
graph: unknown,
message = 'Not in subgraph'
): asserts graph is SubgraphGraph {
if (!isSubgraph(graph)) {
throw new Error(message)
}
}
/**
* Inline assertion for use inside page.evaluate() browser context.
* Returns a string that can be used with Function constructor or eval.
*/
export const SUBGRAPH_ASSERT_INLINE = `
const assertSubgraph = (graph) => {
if (
graph === null ||
typeof graph !== 'object' ||
!('inputs' in graph) ||
!('outputs' in graph) ||
!Array.isArray(graph.inputs) ||
!Array.isArray(graph.outputs)
) {
throw new Error('Not in subgraph');
}
};
`