Skip to content

Commit 57b9ff4

Browse files
authored
Fix types for loaderData and actionData that contain Records (#13139)
1 parent cddb581 commit 57b9ff4

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

.changeset/orange-meals-repair.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Fix types for loaderData and actionData that contained `Record`s
6+
7+
UNSTABLE(BREAKING):
8+
9+
`unstable_SerializesTo` added a way to register custom serialization types in Single Fetch for other library and framework authors like Apollo.
10+
It was implemented with branded type whose branded property that was made optional so that casting arbitrary values was easy:
11+
12+
```ts
13+
// without the brand being marked as optional
14+
let x1 = 42 as unknown as unstable_SerializesTo<number>;
15+
// ^^^^^^^^^^
16+
17+
// with the brand being marked as optional
18+
let x2 = 42 as unstable_SerializesTo<number>;
19+
```
20+
21+
However, this broke type inference in `loaderData` and `actionData` for any `Record` types as those would now (incorrectly) match `unstable_SerializesTo`.
22+
This affected all users, not just those that depended on `unstable_SerializesTo`.
23+
To fix this, the branded property of `unstable_SerializesTo` is marked as required instead of optional.
24+
25+
For library and framework authors using `unstable_SerializesTo`, you may need to add `as unknown` casts before casting to `unstable_SerializesTo`.
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
import type { Equal, Expect } from "./utils";
2+
13
/**
24
* A brand that can be applied to a type to indicate that it will serialize
35
* to a specific type when transported to the client from a loader.
46
* Only use this if you have additional serialization/deserialization logic
57
* in your application.
68
*/
79
export type unstable_SerializesTo<T> = {
8-
unstable__ReactRouter_SerializesTo?: [T];
10+
unstable__ReactRouter_SerializesTo: [T];
911
};
12+
13+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
14+
type __tests = [
15+
Expect<
16+
Equal<
17+
Record<string, any> extends unstable_SerializesTo<any> ? true : false,
18+
false
19+
>
20+
>
21+
];

0 commit comments

Comments
 (0)