diff --git a/.changeset/calm-cycles-dress.md b/.changeset/calm-cycles-dress.md new file mode 100644 index 0000000000..a81205644c --- /dev/null +++ b/.changeset/calm-cycles-dress.md @@ -0,0 +1,13 @@ +--- +"react-router": patch +--- + +Do not rely on `symbol` for filtering out `redirect` responses from loader data + +Previously, some projects were getting type checking errors like: + +```ts +error TS4058: Return type of exported function has or is using name 'redirectSymbol' from external module "node_modules/..." but cannot be named. +``` + +Now that `symbol`s are not used for the `redirect` response type, these errors should no longer be present. diff --git a/packages/react-router/lib/router/utils.ts b/packages/react-router/lib/router/utils.ts index c80dab7ccb..52f7f84452 100644 --- a/packages/react-router/lib/router/utils.ts +++ b/packages/react-router/lib/router/utils.ts @@ -1367,12 +1367,7 @@ export interface TrackedPromise extends Promise { export type RedirectFunction = ( url: string, init?: number | ResponseInit -) => Redirect; - -// use a `unique symbol` to create a branded type -// https://egghead.io/blog/using-branded-types-in-typescript -declare const redirectSymbol: unique symbol; -export type Redirect = Response & { [redirectSymbol]: never }; +) => Response; /** * A redirect response. Sets the status code and the `Location` header. @@ -1391,10 +1386,7 @@ export const redirect: RedirectFunction = (url, init = 302) => { let headers = new Headers(responseInit.headers); headers.set("Location", url); - return new Response(null, { - ...responseInit, - headers, - }) as Redirect; + return new Response(null, { ...responseInit, headers }); }; /** diff --git a/packages/react-router/lib/types/route-data.ts b/packages/react-router/lib/types/route-data.ts index f2d8c41ff7..dafa7f17ac 100644 --- a/packages/react-router/lib/types/route-data.ts +++ b/packages/react-router/lib/types/route-data.ts @@ -2,7 +2,7 @@ import type { ClientLoaderFunctionArgs, ClientActionFunctionArgs, } from "../dom/ssr/routeModules"; -import type { DataWithResponseInit, Redirect } from "../router/utils"; +import type { DataWithResponseInit } from "../router/utils"; import type { Serializable } from "../server-runtime/single-fetch"; import type { Equal, Expect, Func, IsAny, Pretty } from "./utils"; @@ -44,13 +44,13 @@ type DataFrom = // prettier-ignore type ClientData = - T extends Redirect ? never : + T extends Response ? never : T extends DataWithResponseInit ? U : T // prettier-ignore type ServerData = - T extends Redirect ? never : + T extends Response ? never : T extends DataWithResponseInit ? Serialize : Serialize @@ -86,7 +86,7 @@ type __tests = [ | { data: string; b: Date; c: undefined } > >, - Expect { a: string } | Redirect>, { a: string }>>, + Expect { a: string } | Response>, { a: string }>>, // ClientDataFrom Expect, undefined>>, @@ -109,5 +109,5 @@ type __tests = [ | { data: string; b: Date; c: () => boolean } > >, - Expect { a: string } | Redirect>, { a: string }>> + Expect { a: string } | Response>, { a: string }>> ];