Skip to content

Fix types for loaderData and actionData that contain Records #13139

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

Merged
merged 1 commit into from
Feb 28, 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
25 changes: 25 additions & 0 deletions .changeset/orange-meals-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
"react-router": patch
---

Fix types for loaderData and actionData that contained `Record`s

UNSTABLE(BREAKING):

`unstable_SerializesTo` added a way to register custom serialization types in Single Fetch for other library and framework authors like Apollo.
It was implemented with branded type whose branded property that was made optional so that casting arbitrary values was easy:

```ts
// without the brand being marked as optional
let x1 = 42 as unknown as unstable_SerializesTo<number>;
// ^^^^^^^^^^

// with the brand being marked as optional
let x2 = 42 as unstable_SerializesTo<number>;
```

However, this broke type inference in `loaderData` and `actionData` for any `Record` types as those would now (incorrectly) match `unstable_SerializesTo`.
This affected all users, not just those that depended on `unstable_SerializesTo`.
To fix this, the branded property of `unstable_SerializesTo` is marked as required instead of optional.

For library and framework authors using `unstable_SerializesTo`, you may need to add `as unknown` casts before casting to `unstable_SerializesTo`.
14 changes: 13 additions & 1 deletion packages/react-router/lib/types/serializes-to.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import type { Equal, Expect } from "./utils";

/**
* 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];
unstable__ReactRouter_SerializesTo: [T];
};

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type __tests = [
Expect<
Equal<
Record<string, any> extends unstable_SerializesTo<any> ? true : false,
false
>
>
];