Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): better tuple serialization types #6616

Merged
merged 3 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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