@tolki/types 1.3.0 → 1.4.0

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/package.json +1 -1
  2. package/src/routes.d.ts +45 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tolki/types",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Utility TypeScript types for @tolki packages and Laravel HTTP responses.",
5
5
  "author": "Abraham Arango <https://github.com/abetwothree>",
6
6
  "license": "MIT",
package/src/routes.d.ts CHANGED
@@ -260,12 +260,14 @@ export type DefineRouteResultWithArgs<
260
260
  TArgs extends readonly RouteArgMeta[],
261
261
  TComponent extends RouteComponentType | undefined = undefined,
262
262
  TPageProps = never,
263
+ TRequestPayload = never,
263
264
  > = RouteCallableWithArgs<TMethods[0], TArgs> & {
264
265
  url: RouteUrlWithArgs<TArgs>;
265
266
  definition: RouteMetadata<TMethods, TArgs, TComponent, TPageProps>;
266
267
  form: FormWithArgs<TMethods, TArgs>;
267
268
  toString(): string;
268
269
  readonly _pageProps?: TPageProps;
270
+ readonly _requestPayload?: TRequestPayload;
269
271
  } & VerbMethodsWithArgs<TMethods, TArgs> &
270
272
  ComponentMixin<TComponent, TMethods, TArgs>;
271
273
 
@@ -276,12 +278,14 @@ export type DefineRouteResultNoArgs<
276
278
  TMethods extends readonly string[],
277
279
  TComponent extends RouteComponentType | undefined = undefined,
278
280
  TPageProps = never,
281
+ TRequestPayload = never,
279
282
  > = RouteCallableNoArgs<TMethods[0]> & {
280
283
  url: RouteUrlNoArgs;
281
284
  definition: RouteMetadata<TMethods, readonly [], TComponent, TPageProps>;
282
285
  form: FormNoArgs<TMethods>;
283
286
  toString(): string;
284
287
  readonly _pageProps?: TPageProps;
288
+ readonly _requestPayload?: TRequestPayload;
285
289
  } & VerbMethodsNoArgs<TMethods> &
286
290
  ComponentMixin<TComponent, TMethods, readonly []>;
287
291
 
@@ -295,15 +299,29 @@ export type DefineRouteResultNoArgs<
295
299
  * ```typescript
296
300
  * export const dashboard: DefineRouteResult<readonly ['get'], readonly [], 'Dashboard', DashboardPageProps> = defineRoute({...})
297
301
  * ```
302
+ *
303
+ * `TRequestPayload` is a phantom generic for FormRequest payload typing.
304
+ * It defaults to `never` and is populated when a generated route file
305
+ * emits an explicit type annotation:
306
+ * ```typescript
307
+ * export const store: DefineRouteResult<readonly ['post'], readonly [], undefined, never, StorePostRequest> = defineRoute({...})
308
+ * ```
298
309
  */
299
310
  export type DefineRouteResult<
300
311
  TMethods extends readonly string[] = readonly string[],
301
312
  TArgs extends readonly RouteArgMeta[] = readonly RouteArgMeta[],
302
313
  TComponent extends RouteComponentType | undefined = undefined,
303
314
  TPageProps = never,
315
+ TRequestPayload = never,
304
316
  > = [TArgs] extends [readonly []] | [readonly never[]]
305
- ? DefineRouteResultNoArgs<TMethods, TComponent, TPageProps>
306
- : DefineRouteResultWithArgs<TMethods, TArgs, TComponent, TPageProps>;
317
+ ? DefineRouteResultNoArgs<TMethods, TComponent, TPageProps, TRequestPayload>
318
+ : DefineRouteResultWithArgs<
319
+ TMethods,
320
+ TArgs,
321
+ TComponent,
322
+ TPageProps,
323
+ TRequestPayload
324
+ >;
307
325
 
308
326
  /**
309
327
  * The result of calling `.form()` — contains action (URL), method, and toString().
@@ -597,3 +615,28 @@ export type InferPageProps<T> = T extends {
597
615
  ? never
598
616
  : TPageProps
599
617
  : never;
618
+
619
+ /**
620
+ * Infer the request payload type from a `DefineRouteResult`.
621
+ *
622
+ * Returns the `TRequestPayload` generic. Returns `never` when the route const
623
+ * was not annotated with an explicit request payload type (i.e., the default).
624
+ *
625
+ * Reads the `_requestPayload` phantom property directly to avoid TypeScript's
626
+ * deferred conditional-type inference limitations with `DefineRouteResult`.
627
+ *
628
+ * @example
629
+ * ```typescript
630
+ * import type { store } from './post-controller';
631
+ * import type { InferRequestPayload } from '@tolki/ts';
632
+ *
633
+ * type Payload = InferRequestPayload<typeof store>; // StorePostRequest
634
+ * ```
635
+ */
636
+ export type InferRequestPayload<T> = T extends {
637
+ readonly _requestPayload?: infer TRequestPayload;
638
+ }
639
+ ? [TRequestPayload] extends [never]
640
+ ? never
641
+ : TRequestPayload
642
+ : never;