Repository: denoland/fresh
File: src/define.ts (TypeScript)

Code before:
   * data. This function returns the passed input as-is.
   *
   * You can use this function to help the TypeScript compiler infer the types
   * of the data that your page component receives. For example:
   *
   * ```ts
   * import { define } from "../utils.ts";
   *
   * export default define.page((props) => {
   *   const slug = props.params.slug; // Because props is inferred to be a FreshContext object, slug is inferred to be a string
   *   return <h1>{slug}</h1>;
   * });
   * ```
   *
   * This is particularly useful when combined with the {@link handlers}
   * helper function, in which case the data type will be inferred from the
   * return type of the handler method.
   *
   * ```ts
   * import { render } from "@fresh/core";
   * import { define } from "../utils.ts";
   *
   * export const handler = define.handlers({
   *   async GET(ctx) {
   *     const slug = ctx.params.slug; // slug is inferred to be a string
   *     return render({ slug });
   *  },
   * });
   *
   * export default define.page<typeof handler>(({ data }) => {
   *   const slug = data.slug; // slug is inferred to be a string here
   *   return <h1>{slug}</h1>;
   * });
   * ```
   *
   * As a rule of thumb, always use this function to define your page
   * components. If you also have a handler for this route, pass the handler
   * object as a type argument to this function. If you do not have a handler,
   * omit the type argument.
   *
   * @typeParam Handler The type of the handler object that this page component is associated with. If this route has a handler, pass the handler object as a type argument to this function, e.g. `typeof handler`. If this route does not have a handler, omit this type argument.
   * @typeParam Data The type of data that the page component receives. This will be inferred from the handler methods if not provided. In very advanced use cases, you can specify `never` to the `Handler` type argument and provide the `Data` type explicitly.
   */
  page<
    // deno-lint-ignore no-explicit-any
    Handler extends RouteHandler<any, State> = never,
    Data = Handler extends HandlerByMethod<infer Data, State> ? Data : never,
  >(
    render: AnyComponent<FreshContext<Data, State> & { Component: () => null }>,
  ): typeof render;


Review comment (suggestion):
```suggestion
   *     return page({ slug });
```

Code after:
   * data. This function returns the passed input as-is.
   *
   * You can use this function to help the TypeScript compiler infer the types
   * of the data that your page component receives. For example:
   *
   * ```ts
   * import { define } from "../utils.ts";
   *
   * export default define.page((props) => {
   *   const slug = props.params.slug; // Because props is inferred to be a FreshContext object, slug is inferred to be a string
   *   return <h1>{slug}</h1>;
   * });
   * ```
   *
   * This is particularly useful when combined with the {@link handlers}
   * helper function, in which case the data type will be inferred from the
   * return type of the handler method.
   *
   * ```ts
   * import { page } from "@fresh/core";
   * import { define } from "../utils.ts";
   *
   * export const handler = define.handlers({
   *   async GET(ctx) {
   *     const slug = ctx.params.slug; // slug is inferred to be a string
   *     return page({ slug });
   *  },
   * });
   *
   * export default define.page<typeof handler>(({ data }) => {
   *   const slug = data.slug; // slug is inferred to be a string here
   *   return <h1>{slug}</h1>;
   * });
   * ```
   *
   * As a rule of thumb, always use this function to define your page
   * components. If you also have a handler for this route, pass the handler
   * object as a type argument to this function. If you do not have a handler,
   * omit the type argument.
   *
   * @typeParam Handler The type of the handler object that this page component is associated with. If this route has a handler, pass the handler object as a type argument to this function, e.g. `typeof handler`. If this route does not have a handler, omit this type argument.
   * @typeParam Data The type of data that the page component receives. This will be inferred from the handler methods if not provided. In very advanced use cases, you can specify `never` to the `Handler` type argument and provide the `Data` type explicitly.
   */
  page<
    // deno-lint-ignore no-explicit-any
    Handler extends RouteHandler<any, State> = never,
    Data = Handler extends HandlerByMethod<infer Data, State> ? Data : never,
  >(
    render: AnyComponent<FreshContext<Data, State> & { Component: () => null }>,
  ): typeof render;