@sanity/codegen 5.5.1-next.3 → 5.6.0-next.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.d.ts +162 -0
  2. package/package.json +6 -6
package/lib/index.d.ts CHANGED
@@ -82,6 +82,53 @@ export declare interface ExtractedQuery {
82
82
  filename: string
83
83
  }
84
84
 
85
+ /**
86
+ * Filter a union of object types by the _type property. This is handy when working with page builder
87
+ * setups where the returned type is an array containing multiple types.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ *
92
+ * export type Callout = {
93
+ * _type: 'callout'
94
+ * title?: string
95
+ * content?: string
96
+ * }
97
+ *
98
+ * export type Video = {
99
+ * _type: 'video'
100
+ * url?: string
101
+ * caption?: string
102
+ * }
103
+ * type FORM_QUERY_RESULT = {
104
+ * _id: string
105
+ * title?: string
106
+ * content?: Array<
107
+ * | ({ _key: string } & Callout)
108
+ * | ({ _key: string } & Video)
109
+ * >
110
+ * } | null
111
+ *
112
+ * // Get the type of the content with the Get helper
113
+ * type Content = Get<FORM_QUERY_RESULT, 'content', number>
114
+ *
115
+ * // Get the type for a callout module from the page builder type
116
+ * type CalloutModule = FilterByType<Content, 'callout'>
117
+ * // → { _key: string } & Callout
118
+ * ```
119
+ */
120
+ export declare type FilterByType<
121
+ U extends {
122
+ _type: string
123
+ },
124
+ T extends U['_type'],
125
+ > = Extract<
126
+ U,
127
+ {
128
+ _type: T
129
+ }
130
+ >
131
+
85
132
  /**
86
133
  * findQueriesInPath takes a path or array of paths and returns all GROQ queries in the files.
87
134
  * @param path - The path or array of paths to search for queries
@@ -132,6 +179,102 @@ export declare interface GenerateTypesOptions {
132
179
  reporter?: WorkerChannelReporter<TypegenWorkerChannel>
133
180
  }
134
181
 
182
+ /**
183
+ * Get a deeply nested property type from a complex type structure. Safely navigates
184
+ * through nullable types (`T | null | undefined`) at each level, preserving the
185
+ * nullability of the final accessed property.
186
+ *
187
+ * Supports up to 20 levels of nesting.
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * type POST_QUERY_RESULT = {
192
+ * _id: string
193
+ * author: {
194
+ * profile: {
195
+ * name: string;
196
+ * } | null;
197
+ * } | null;
198
+ * links: Array<{
199
+ * _key: string
200
+ * type: 'link'
201
+ * label: string
202
+ * url: string
203
+ * }> | null
204
+ * } | null
205
+ *
206
+ * // Basic property access:
207
+ * type Id = Get<POST_QUERY_RESULT, '_id'>;
208
+ * // → string
209
+ *
210
+ * // Nested property access:
211
+ * type Profile = Get<POST_QUERY_RESULT, 'author', 'profile';
212
+ * // → { name: string } | null
213
+ *
214
+ * // Array element access using `number`:
215
+ * type Link = Get<POST_QUERY_RESULT, 'links', number, 'label'>;
216
+ * // → string
217
+ * ```
218
+ */
219
+ export declare type Get<
220
+ T,
221
+ K1 extends keyof NonNullish<T>,
222
+ K2 extends keyof NavigatePath<T, [K1]> = never,
223
+ K3 extends keyof NavigatePath<T, [K1, K2]> = never,
224
+ K4 extends keyof NavigatePath<T, [K1, K2, K3]> = never,
225
+ K5 extends keyof NavigatePath<T, [K1, K2, K3, K4]> = never,
226
+ K6 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5]> = never,
227
+ K7 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6]> = never,
228
+ K8 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7]> = never,
229
+ K9 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8]> = never,
230
+ K10 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9]> = never,
231
+ K11 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10]> = never,
232
+ K12 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11]> = never,
233
+ K13 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12]> = never,
234
+ K14 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13]> =
235
+ never,
236
+ K15 extends keyof NavigatePath<T, [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14]> =
237
+ never,
238
+ K16 extends keyof NavigatePath<
239
+ T,
240
+ [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15]
241
+ > = never,
242
+ K17 extends keyof NavigatePath<
243
+ T,
244
+ [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16]
245
+ > = never,
246
+ K18 extends keyof NavigatePath<
247
+ T,
248
+ [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16, K17]
249
+ > = never,
250
+ K19 extends keyof NavigatePath<
251
+ T,
252
+ [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16, K17, K18]
253
+ > = never,
254
+ K20 extends keyof NavigatePath<
255
+ T,
256
+ [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16, K17, K18, K19]
257
+ > = never,
258
+ > = GetAtPath<
259
+ T,
260
+ TakeUntilNever<
261
+ [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K20]
262
+ >
263
+ >
264
+
265
+ /** Recursively gets value at path, preserving nullability at final access. */
266
+ declare type GetAtPath<T, Path extends unknown[]> = Path extends []
267
+ ? T
268
+ : Path extends [infer K]
269
+ ? K extends keyof NonNullish<T>
270
+ ? NonNullish<T>[K]
271
+ : never
272
+ : Path extends [infer K, ...infer Rest]
273
+ ? K extends keyof NonNullish<T>
274
+ ? GetAtPath<NonNullish<T>[K], Rest>
275
+ : never
276
+ : never
277
+
135
278
  /**
136
279
  * This is a custom implementation of require.resolve that takes into account the paths
137
280
  * configuration in tsconfig.json. This is necessary if we want to resolve paths that are
@@ -141,6 +284,18 @@ export declare interface GenerateTypesOptions {
141
284
  */
142
285
  export declare function getResolver(cwd?: string): NodeJS.RequireResolve
143
286
 
287
+ /** Recursively navigates through a path, stripping nullability for key lookup. */
288
+ declare type NavigatePath<T, Path extends unknown[]> = Path extends []
289
+ ? NonNullish<T>
290
+ : Path extends [infer K, ...infer Rest]
291
+ ? K extends keyof NonNullish<T>
292
+ ? NavigatePath<NonNullish<T>[K], Rest>
293
+ : never
294
+ : never
295
+
296
+ /** Excludes `null` and `undefined` from a type. */
297
+ declare type NonNullish<T> = T extends null | undefined ? never : T
298
+
144
299
  /**
145
300
  * An error that occurred during query evaluation.
146
301
  * @public
@@ -209,6 +364,13 @@ export declare function registerBabel(babelOptions?: TransformOptions): void
209
364
  */
210
365
  export declare function safeParseQuery(query: string): ExprNode
211
366
 
367
+ /** Builds a tuple from elements, stopping at the first `never`. */
368
+ declare type TakeUntilNever<T extends unknown[]> = T extends [infer H, ...infer Rest]
369
+ ? [H] extends [never]
370
+ ? []
371
+ : [H, ...TakeUntilNever<Rest>]
372
+ : []
373
+
212
374
  /**
213
375
  * Statistics from the query type evaluation process.
214
376
  * @public
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/codegen",
3
- "version": "5.5.1-next.3+c86d4fb254",
3
+ "version": "5.6.0-next.12+682b253b5b",
4
4
  "description": "Codegen toolkit for Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -50,7 +50,7 @@
50
50
  "reselect": "^5.1.1",
51
51
  "tsconfig-paths": "^4.2.0",
52
52
  "zod": "^3.25.76",
53
- "groq": "5.5.1-next.3+c86d4fb254"
53
+ "groq": "5.6.0-next.12+682b253b5b"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@sanity/pkg-utils": "^10.3.2",
@@ -64,10 +64,10 @@
64
64
  "eslint": "^9.39.2",
65
65
  "rimraf": "^5.0.10",
66
66
  "vitest": "^4.0.16",
67
- "@repo/test-config": "5.5.1-next.3+c86d4fb254",
68
- "@repo/package.config": "5.5.1-next.3+c86d4fb254",
69
- "@repo/tsconfig": "5.5.1-next.3+c86d4fb254",
70
- "@repo/eslint-config": "5.5.1-next.3+c86d4fb254"
67
+ "@repo/eslint-config": "5.6.0-next.12+682b253b5b",
68
+ "@repo/test-config": "5.6.0-next.12+682b253b5b",
69
+ "@repo/package.config": "5.6.0-next.12+682b253b5b",
70
+ "@repo/tsconfig": "5.6.0-next.12+682b253b5b"
71
71
  },
72
72
  "engines": {
73
73
  "node": ">=20.19 <22 || >=22.12"