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

feat(react-router): add SerializesTo brand type #12264

Merged
merged 8 commits into from
Feb 12, 2025
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
5 changes: 5 additions & 0 deletions .changeset/sour-avocados-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Add `unstable_SerializesTo` brand type for library authors to register types serializable by React Router's streaming format (`turbo-stream`)
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
- pavsoldatov
- pcattori
- petersendidit
- phryneas
- phildl
- pierophp
- printfn
Expand Down
1 change: 1 addition & 0 deletions packages/react-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ export type {
FlashSessionData,
} from "./lib/server-runtime/sessions";

export type { unstable_SerializesTo } from "./lib/types/serializes-to.ts";
export type { Register } from "./lib/types/register";
export { href } from "./lib/href";

Expand Down
35 changes: 28 additions & 7 deletions packages/react-router/lib/types/route-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import type {
} from "../dom/ssr/routeModules";
import type { DataWithResponseInit } from "../router/utils";
import type { Serializable } from "../server-runtime/single-fetch";
import type { unstable_SerializesTo } from "./serializes-to";
import type { Equal, Expect, Func, IsAny, Pretty } from "./utils";

// prettier-ignore
type Serialize<T> =
// First, let type stay as-is if its already serializable...
// If type has a `SerializesTo` brand, use that type
T extends unstable_SerializesTo<infer To> ? To :

// Then, let type stay as-is if its already serializable...
T extends Serializable ? T :

// ...then don't allow functions to be serialized...
Expand Down Expand Up @@ -69,21 +73,38 @@ type __tests = [
Expect<Equal<ServerDataFrom<any>, undefined>>,
Expect<
Equal<
ServerDataFrom<() => { a: string; b: Date; c: () => boolean }>,
{ a: string; b: Date; c: undefined }
ServerDataFrom<
() => {
a: string;
b: Date;
c: () => boolean;
d: unstable_SerializesTo<number>;
}
>,
{ a: string; b: Date; c: undefined; d: number }
>
>,
Expect<
Equal<
Pretty<
ServerDataFrom<
() =>
| { json: string; b: Date; c: () => boolean }
| DataWithResponseInit<{ data: string; b: Date; c: () => boolean }>
| {
json: string;
b: Date;
c: () => boolean;
d: unstable_SerializesTo<number>;
}
| DataWithResponseInit<{
data: string;
b: Date;
c: () => boolean;
d: unstable_SerializesTo<number>;
}>
>
>,
| { json: string; b: Date; c: undefined }
| { data: string; b: Date; c: undefined }
| { json: string; b: Date; c: undefined; d: number }
| { data: string; b: Date; c: undefined; d: number }
>
>,
Expect<Equal<ServerDataFrom<() => { a: string } | Response>, { a: string }>>,
Expand Down
9 changes: 9 additions & 0 deletions packages/react-router/lib/types/serializes-to.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* A brand that can be applied to a type to indicate that it will serialize
* to a specific type when transported to the client from a loader.
* Only use this if you have additional serialization/deserialization logic
* in your application.
*/
export type unstable_SerializesTo<T> = {
unstable__ReactRouter_SerializesTo?: [T];
};