From 30cedcdf5a550022bedfd4a5a0a8fd38d9914862 Mon Sep 17 00:00:00 2001 From: Pedro Cattori Date: Mon, 22 May 2023 09:47:46 -0400 Subject: [PATCH] feat: simplified types for Serialize --- .changeset/lemon-beers-fetch.md | 24 ++++++++++++++++++++++ packages/remix-server-runtime/serialize.ts | 8 ++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 .changeset/lemon-beers-fetch.md diff --git a/.changeset/lemon-beers-fetch.md b/.changeset/lemon-beers-fetch.md new file mode 100644 index 00000000000..0e9c3032f41 --- /dev/null +++ b/.changeset/lemon-beers-fetch.md @@ -0,0 +1,24 @@ +--- +"@remix-run/server-runtime": minor +"@remix-run/react": minor +--- + +Force Typescript to simplify type produced by `Serialize`. + +As a result, the following types and functions have simplified return types: +- SerializeFrom +- useLoaderData +- useActionData +- useFetcher + +```ts +type Data = { hello: string; when: Date } + +// BEFORE +type Unsimplified = SerializeFrom +// ^? SerializeObject> + +// AFTER +type Simplified = SerializeFrom +// ^? { hello: string; when: string } +``` diff --git a/packages/remix-server-runtime/serialize.ts b/packages/remix-server-runtime/serialize.ts index d11cbeb169e..d418bc07288 100644 --- a/packages/remix-server-runtime/serialize.ts +++ b/packages/remix-server-runtime/serialize.ts @@ -1,6 +1,9 @@ import type { AppData } from "./data"; import type { TypedDeferredData, TypedResponse } from "./responses"; +// force Typescript to simplify the type +type Pretty = { [K in keyof T]: T[K] } & {}; + type JsonPrimitive = | string | number @@ -18,7 +21,7 @@ type NonJsonPrimitive = undefined | Function | symbol; type IsAny = 0 extends 1 & T ? true : false; // prettier-ignore -type Serialize = +type Serialize = Pretty< IsAny extends true ? any : T extends TypedDeferredData ? SerializeDeferred : T extends JsonPrimitive ? T : @@ -28,7 +31,8 @@ type Serialize = T extends [unknown, ...unknown[]] ? SerializeTuple : T extends ReadonlyArray ? (U extends NonJsonPrimitive ? null : Serialize)[] : T extends object ? SerializeObject> : - never; + never +>; /** JSON serialize [tuples](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) */ type SerializeTuple = {