Skip to content

Commit 372e25f

Browse files
authored
do not rely on symbol for filtering out redirects from loader data (#12694)
1 parent ebc8241 commit 372e25f

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

.changeset/calm-cycles-dress.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"react-router": patch
3+
---
4+
5+
Do not rely on `symbol` for filtering out `redirect` responses from loader data
6+
7+
Previously, some projects were getting type checking errors like:
8+
9+
```ts
10+
error TS4058: Return type of exported function has or is using name 'redirectSymbol' from external module "node_modules/..." but cannot be named.
11+
```
12+
13+
Now that `symbol`s are not used for the `redirect` response type, these errors should no longer be present.

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,12 +1367,7 @@ export interface TrackedPromise extends Promise<any> {
13671367
export type RedirectFunction = (
13681368
url: string,
13691369
init?: number | ResponseInit
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 };
1370+
) => Response;
13761371

13771372
/**
13781373
* A redirect response. Sets the status code and the `Location` header.
@@ -1391,10 +1386,7 @@ export const redirect: RedirectFunction = (url, init = 302) => {
13911386
let headers = new Headers(responseInit.headers);
13921387
headers.set("Location", url);
13931388

1394-
return new Response(null, {
1395-
...responseInit,
1396-
headers,
1397-
}) as Redirect;
1389+
return new Response(null, { ...responseInit, headers });
13981390
};
13991391

14001392
/**

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

Lines changed: 5 additions & 5 deletions
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, Redirect } from "../router/utils";
5+
import type { DataWithResponseInit } from "../router/utils";
66
import type { Serializable } from "../server-runtime/single-fetch";
77
import type { Equal, Expect, Func, IsAny, Pretty } from "./utils";
88

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

4545
// prettier-ignore
4646
type ClientData<T> =
47-
T extends Redirect ? never :
47+
T extends Response ? never :
4848
T extends DataWithResponseInit<infer U> ? U :
4949
T
5050

5151
// prettier-ignore
5252
type ServerData<T> =
53-
T extends Redirect ? never :
53+
T extends Response ? never :
5454
T extends DataWithResponseInit<infer U> ? Serialize<U> :
5555
Serialize<T>
5656

@@ -86,7 +86,7 @@ type __tests = [
8686
| { data: string; b: Date; c: undefined }
8787
>
8888
>,
89-
Expect<Equal<ServerDataFrom<() => { a: string } | Redirect>, { a: string }>>,
89+
Expect<Equal<ServerDataFrom<() => { a: string } | Response>, { a: string }>>,
9090

9191
// ClientDataFrom
9292
Expect<Equal<ClientDataFrom<any>, undefined>>,
@@ -109,5 +109,5 @@ type __tests = [
109109
| { data: string; b: Date; c: () => boolean }
110110
>
111111
>,
112-
Expect<Equal<ClientDataFrom<() => { a: string } | Redirect>, { a: string }>>
112+
Expect<Equal<ClientDataFrom<() => { a: string } | Response>, { a: string }>>
113113
];

0 commit comments

Comments
 (0)