Skip to content

Commit

Permalink
fix(types): better tuple serialization types (#6616)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori authored Jun 16, 2023
1 parent 4b1e80f commit 3c4a6a4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/neat-poets-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"remix": patch
"@remix-run/serve": patch
"@remix-run/server-runtime": patch
---

fix(types): better tuple serialization types
8 changes: 4 additions & 4 deletions packages/remix-server-runtime/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ type Serialize<T> =
T extends NonJsonPrimitive ? never :
T extends { toJSON(): infer U } ? U :
T extends [] ? [] :
T extends [unknown, ...unknown[]] ? SerializeTuple<T> :
T extends [unknown, ...unknown[]] ? PrettyTransform<T, SerializeTuple<T>> :
T extends ReadonlyArray<infer U> ? (U extends NonJsonPrimitive ? null : Serialize<U>)[] :
T extends object ? PrettyTransform<T, SerializeObject<UndefinedToOptional<T>>> :
never
;

/** JSON serialize [tuples](https://www.typescriptlang.org/docs/handbook/2/objects.html#tuple-types) */
type SerializeTuple<T extends [unknown, ...unknown[]]> = {
[k in keyof T]: T[k] extends NonJsonPrimitive ? null : Serialize<T[k]>;
};
type SerializeTuple<T extends unknown[]> = T extends [infer F, ...infer R]
? [Serialize<F>, ...SerializeTuple<R>]
: [];

/** JSON serialize objects (not including arrays) and classes */
type SerializeObject<T extends object> = {
Expand Down

0 comments on commit 3c4a6a4

Please sign in to comment.