Skip to content

Commit c9487e6

Browse files
authored
ignore redirects when inferring loader data types (#12527)
1 parent 7cd5c54 commit c9487e6

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

.changeset/khaki-gifts-grin.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Ignore redirects when inferring loader data types

packages/react-router/lib/router/utils.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,12 @@ export interface TrackedPromise extends Promise<any> {
13671367
export type RedirectFunction = (
13681368
url: string,
13691369
init?: number | ResponseInit
1370-
) => Response;
1370+
) => Redirect;
1371+
1372+
// use a `unique symbol` to create a branded type
1373+
// https://egghead.io/blog/using-branded-types-in-typescript
1374+
declare const redirectSymbol: unique symbol;
1375+
export type Redirect = Response & { [redirectSymbol]: never };
13711376

13721377
/**
13731378
* A redirect response. Sets the status code and the `Location` header.
@@ -1389,7 +1394,7 @@ export const redirect: RedirectFunction = (url, init = 302) => {
13891394
return new Response(null, {
13901395
...responseInit,
13911396
headers,
1392-
});
1397+
}) as Redirect;
13931398
};
13941399

13951400
/**

packages/react-router/lib/types/route-data.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {
22
ClientLoaderFunctionArgs,
33
ClientActionFunctionArgs,
44
} from "../dom/ssr/routeModules";
5-
import type { DataWithResponseInit } from "../router/utils";
5+
import type { DataWithResponseInit, Redirect } from "../router/utils";
66
import type { Serializable } from "../server-runtime/single-fetch";
77
import type { Equal, Expect, Func, IsAny, Pretty } from "./utils";
88

@@ -44,11 +44,13 @@ type DataFrom<T> =
4444

4545
// prettier-ignore
4646
type ClientData<T> =
47+
T extends Redirect ? never :
4748
T extends DataWithResponseInit<infer U> ? U :
4849
T
4950

5051
// prettier-ignore
5152
type ServerData<T> =
53+
T extends Redirect ? never :
5254
T extends DataWithResponseInit<infer U> ? Serialize<U> :
5355
Serialize<T>
5456

@@ -84,6 +86,7 @@ type __tests = [
8486
| { data: string; b: Date; c: undefined }
8587
>
8688
>,
89+
Expect<Equal<ServerDataFrom<() => { a: string } | Redirect>, { a: string }>>,
8790

8891
// ClientDataFrom
8992
Expect<Equal<ClientDataFrom<any>, undefined>>,
@@ -105,5 +108,6 @@ type __tests = [
105108
| { json: string; b: Date; c: () => boolean }
106109
| { data: string; b: Date; c: () => boolean }
107110
>
108-
>
111+
>,
112+
Expect<Equal<ClientDataFrom<() => { a: string } | Redirect>, { a: string }>>
109113
];

0 commit comments

Comments
 (0)