Repository: remix-run/react-router
File: packages/react-router/lib/router/define-route.ts (TypeScript)

Code before:
  request: Request;
  params: Params<Param>;
  response: ResponseStub;
};

// prettier-ignore
type Params<Param extends string> = Pretty<
  & {[key: string]: string | undefined}
  & {[K in Param as K extends `${string}?` ? never : K]: string}
  & {[K in Param as K extends `${infer P}?` ? P : never]?: string}
>

type IsDefined<T> = undefined extends T ? false : true;

// prettier-ignore
type _LoaderData<
  ServerLoaderData,
  ClientLoaderData,
  ClientLoaderHydrate extends boolean,
  HydrateFallback,
> = Awaited<
  [IsDefined<HydrateFallback>, ClientLoaderHydrate]  extends [true, true] ?
    IsDefined<ClientLoaderData> extends true ? ClientLoaderData :
    undefined
  :
  [IsDefined<ClientLoaderData>, IsDefined<ServerLoaderData>] extends [true, true] ? ServerLoaderData | ClientLoaderData :
  IsDefined<ClientLoaderData> extends true ?
    IsDefined<ClientLoaderHydrate> extends true ? ClientLoaderData :
    ClientLoaderData | undefined
  :
  IsDefined<ServerLoaderData> extends true ? ServerLoaderData :
  undefined
>

type LoaderData<
  ServerLoaderData,
  ClientLoaderData,
  ClientLoaderHydrate extends boolean,
  HydrateFallback
> = _LoaderData<
  Awaited<ServerLoaderData>,
  Awaited<ClientLoaderData>,
  ClientLoaderHydrate,
  HydrateFallback
>;

// prettier-ignore
type ActionData<ServerActionData, ClientActionData> = Awaited<
  [IsDefined<ServerActionData>, IsDefined<ClientActionData>] extends [true, true] ? ServerActionData | ClientActionData :
  IsDefined<ClientActionData> extends true ? ClientActionData :
  IsDefined<ServerActionData> extends true ? ServerActionData :

Review comment (bug):
This was a bug that didn't show up until I added tests for returning `undefined`. In short `ClientLoaderHydrate` is a `boolean` not a data type, so it should be checked directly.

Code after:
  response: ResponseStub;
};

// prettier-ignore
type Params<Param extends string> = Pretty<
  & {[key: string]: string | undefined}
  & {[K in Param as K extends `${string}?` ? never : K]: string}
  & {[K in Param as K extends `${infer P}?` ? P : never]?: string}
>

type IsDefined<T> = Equal<T, undefined> extends true ? false : true;

// prettier-ignore
type _LoaderData<
  ServerLoaderData,
  ClientLoaderData,
  ClientLoaderHydrate extends boolean,
  HydrateFallback,
> = Awaited<
  [undefined extends HydrateFallback ? false : true, ClientLoaderHydrate]  extends [true, true] ?
    IsDefined<ClientLoaderData> extends true ? ClientLoaderData :
    undefined
  :
  [IsDefined<ClientLoaderData>, IsDefined<ServerLoaderData>] extends [true, true] ? ServerLoaderData | ClientLoaderData :
  IsDefined<ClientLoaderData> extends true ?
    ClientLoaderHydrate extends true ? ClientLoaderData :
    ClientLoaderData | undefined
  :
  IsDefined<ServerLoaderData> extends true ? ServerLoaderData :
  undefined
>

type LoaderData<
  ServerLoaderData,
  ClientLoaderData,
  ClientLoaderHydrate extends boolean,
  HydrateFallback
> = _LoaderData<
  Awaited<ServerLoaderData>,
  Awaited<ClientLoaderData>,
  ClientLoaderHydrate,
  HydrateFallback
>;

// prettier-ignore
type ActionData<ServerActionData, ClientActionData> = Awaited<
  [IsDefined<ServerActionData>, IsDefined<ClientActionData>] extends [true, true] ? ServerActionData | ClientActionData :
  IsDefined<ClientActionData> extends true ? ClientActionData :
  IsDefined<ServerActionData> extends true ? ServerActionData :
  undefined
>