11import type { OutputKey } from "./OutputKey" ;
22import type { OutputAccessor , InferOutputEntry } from "./OutputAccessor" ;
3+ import type { z } from "zod" ;
4+
5+ /**
6+ * Reverse-lookup: given Schema and a value type V, find the key K where Schema[K] extends V.
7+ * Used to narrow return types when passing Zod schema objects directly.
8+ */
9+ type SchemaKeyForValue < Schema , V > = {
10+ [ K in keyof Schema & string ] : Schema [ K ] extends V ? K : never ;
11+ } [ keyof Schema & string ] ;
312
413export interface SmithersCtx < Schema > {
514 runId : string ;
@@ -8,22 +17,49 @@ export interface SmithersCtx<Schema> {
817 input : Schema extends { input : infer T } ? T : Record < string , unknown > ;
918 outputs : OutputAccessor < Schema > ;
1019
20+ // Overload: pass Zod schema value directly → narrowed return type
21+ output < V extends z . ZodTypeAny > (
22+ table : V ,
23+ key : OutputKey ,
24+ ) : SchemaKeyForValue < Schema , V > extends never
25+ ? InferOutputEntry < V >
26+ : InferOutputEntry < V > ;
27+
28+ // Overload: pass string key → narrowed via K
1129 output < K extends keyof Schema & string > (
12- table : K | Schema [ K ] ,
30+ table : K ,
1331 key : OutputKey ,
1432 ) : InferOutputEntry < Schema [ K ] > ;
1533
34+ // Overload: pass Zod schema value directly → narrowed return type
35+ outputMaybe < V extends z . ZodTypeAny > (
36+ table : V ,
37+ key : OutputKey ,
38+ ) : SchemaKeyForValue < Schema , V > extends never
39+ ? InferOutputEntry < V > | undefined
40+ : InferOutputEntry < V > | undefined ;
41+
42+ // Overload: pass string key → narrowed via K
1643 outputMaybe < K extends keyof Schema & string > (
17- table : K | Schema [ K ] ,
44+ table : K ,
1845 key : OutputKey ,
1946 ) : InferOutputEntry < Schema [ K ] > | undefined ;
2047
48+ // Overload: pass Zod schema value directly → narrowed return type
49+ latest < V extends z . ZodTypeAny > (
50+ table : V ,
51+ nodeId : string ,
52+ ) : SchemaKeyForValue < Schema , V > extends never
53+ ? InferOutputEntry < V > | undefined
54+ : InferOutputEntry < V > | undefined ;
55+
56+ // Overload: pass string key → narrowed via K
2157 latest < K extends keyof Schema & string > (
22- table : K | Schema [ K ] ,
58+ table : K ,
2359 nodeId : string ,
2460 ) : InferOutputEntry < Schema [ K ] > | undefined ;
2561
26- latestArray ( value : unknown , schema : import ( "zod" ) . ZodType ) : any [ ] ;
62+ latestArray ( value : unknown , schema : z . ZodType ) : any [ ] ;
2763
2864 iterationCount ( table : any , nodeId : string ) : number ;
2965}
0 commit comments