Skip to content
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

feat!: align types with React Router #7319

Merged
merged 10 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update Remix types to use unknown instead of any
  • Loading branch information
brophdawg11 committed Sep 5, 2023
commit a389bafe95668f9788b4965b0a1a50299d58efd6
16 changes: 12 additions & 4 deletions .changeset/align-rr-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
"@remix-run/server-runtime": major
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved
---

Remove/Align Remix types with those use din React Router
Remove/align Remix types with those used in React Router

* Remove Remix definitions of `LoaderFunction`/`ActionFunction` and re-export from React Router
* Remove `LoaderArgs`/`ActionArgs` and re-export `LoaderFunctionArgs`/`ActionFunctionArgs` from React Router
* Change `AppLoadContext` from a keyed object in Remix to the `any` used in React Router to permit users to return anything from `getLoadContext`
* Change exposed `any` types to `unknown`
* `AppData`
* `useLocation.state`
* `useMatches()[i].data`
* `useFetcher().data`
* `MetaMatch.handle`
* `useMatches()[i].handle` type changed from `{ [k: string]: any }` to `unknown`
* `AppLoadContext` type changed from `{ [k: string]: unknown }` to `unknown`
* Make `LoaderFunctionArgs`/`ActionFunctionArgs` generic to accept a `context` type
* Rename `LoaderFunction`/`ActionFunction` to `LoaderFunctionArgs`/`ActionFunctionArgs`
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved
* Rename the `useMatches()` return type from `RouteMatch` to `UIMatch`
41 changes: 13 additions & 28 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import type {
AgnosticDataRouteMatch,
UNSAFE_DeferredData as DeferredData,
TrackedPromise,
UIMatch as UIMatchRR,
} from "@remix-run/router";
import type {
FetcherWithComponents,
LinkProps,
NavLinkProps,
Params,
} from "react-router-dom";
import {
Await as AwaitRR,
Expand All @@ -26,6 +26,7 @@ import {
useActionData as useActionDataRR,
useFetcher as useFetcherRR,
useLoaderData as useLoaderDataRR,
useMatches as useMatchesRR,
useRouteLoaderData as useRouteLoaderDataRR,
useLocation,
useNavigation,
Expand Down Expand Up @@ -975,32 +976,16 @@ function dedupe(array: any[]) {
return [...new Set(array)];
}

// TODO: Can this be re-exported from RR?
export interface RouteMatch {
/**
* The id of the matched route
*/
id: string;
/**
* The pathname of the matched route
*/
pathname: string;
/**
* The dynamic parameters of the matched route
*
* @see https://remix.run/file-conventions/routes-files#dynamic-route-parameters
*/
params: Params<string>;
/**
* Any route data associated with the matched route
*/
data: any;
/**
* The exported `handle` object of the matched route.
*
* @see https://remix.run/route/handle
*/
handle: undefined | { [key: string]: any };
export interface UIMatch<D = AppData> extends UIMatchRR<SerializeFrom<D>> {}
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the active route matches, useful for accessing loaderData for
* parent/child routes or the route "handle" property
*
* @see https://remix.run/docs/hooks/use-matches
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved
*/
export function useMatches(): UIMatch[] {
return useMatchesRR() as UIMatch[];
}

/**
Expand Down Expand Up @@ -1038,7 +1023,7 @@ export function useActionData<T = AppData>(): SerializeFrom<T> | undefined {
*
* @see https://remix.run/hooks/use-fetcher
*/
export function useFetcher<TData = any>(): FetcherWithComponents<
export function useFetcher<TData = unknown>(): FetcherWithComponents<
SerializeFrom<TData>
> {
return useFetcherRR();
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-react/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
/**
* Data for a route that was returned from a `loader()`.
*/
export type AppData = any;
export type AppData = unknown;
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved

export function isCatchResponse(response: Response): boolean {
return response.headers.get("X-Remix-Catch") != null;
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export {

export type {
AwaitProps,
RouteMatch,
RemixNavLinkProps as NavLinkProps,
RemixLinkProps as LinkProps,
UIMatch,
} from "./components";
export {
Await,
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"typings": "dist/index.d.ts",
"module": "dist/esm/index.js",
"dependencies": {
"@remix-run/router": "1.9.0-pre.0",
"@remix-run/router": "1.9.0-pre.1",
"@remix-run/server-runtime": "2.0.0-pre.4",
"react-router-dom": "6.16.0-pre.0"
"react-router-dom": "6.16.0-pre.1"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.17.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-react/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface MetaMatch<
id: RouteId;
pathname: DataRouteMatch["pathname"];
data: Loader extends LoaderFunction ? SerializeFrom<Loader> : unknown;
handle?: unknown;
handle?: RouteHandle;
params: DataRouteMatch["params"];
meta: MetaDescriptor[];
error?: unknown;
Expand Down Expand Up @@ -121,7 +121,7 @@ export type RouteComponent = ComponentType<{}>;
*
* @see https://remix.run/route/handle
*/
export type RouteHandle = any;
export type RouteHandle = unknown;
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved

export async function loadRouteModule(
route: EntryRoute,
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-server-runtime/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import type { DataFunctionArgs } from "./routeModules";
* An unknown type for route loaders and actions provided by the server's
* `getLoadContext()` function.
*/
export type AppLoadContext = any;
export type AppLoadContext = unknown;
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Data for a route that was returned from a `loader()`.
*/
export type AppData = any;
export type AppData = unknown;
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved

export async function callRouteActionRR({
loadContext,
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-server-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"typings": "dist/index.d.ts",
"module": "dist/esm/index.js",
"dependencies": {
"@remix-run/router": "1.9.0-pre.0",
"@remix-run/router": "1.9.0-pre.1",
"@types/cookie": "^0.4.1",
"@web3-storage/multipart-parser": "^1.0.0",
"cookie": "^0.4.1",
Expand Down
16 changes: 10 additions & 6 deletions packages/remix-server-runtime/routeModules.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type {
ActionFunction,
ActionFunctionArgs,
ActionFunction as RRActionFunction,
ActionFunctionArgs as RRActionFunctionArgs,
AgnosticRouteMatch,
LoaderFunction,
LoaderFunctionArgs,
LoaderFunction as RRLoaderFunction,
LoaderFunctionArgs as RRLoaderFunctionArgs,
Location,
Params,
} from "@remix-run/router";
Expand All @@ -16,6 +16,10 @@ export interface RouteModules<RouteModule> {
[routeId: string]: RouteModule;
}

export type ActionFunctionArgs = RRActionFunctionArgs<unknown>;
export type ActionFunction = RRActionFunction<unknown>;
export type LoaderFunctionArgs = RRLoaderFunctionArgs<unknown>;
export type LoaderFunction = RRLoaderFunction<unknown>;
export type DataFunctionArgs = LoaderFunctionArgs | ActionFunctionArgs;

export type HeadersArgs = {
Expand Down Expand Up @@ -118,7 +122,7 @@ interface ServerRuntimeMetaMatch<
id: RouteId;
pathname: AgnosticRouteMatch["pathname"];
data: Loader extends LoaderFunction ? SerializeFrom<Loader> : unknown;
handle?: unknown;
handle?: RouteHandle;
params: AgnosticRouteMatch["params"];
meta: ServerRuntimeMetaDescriptor[];
error?: unknown;
Expand Down Expand Up @@ -173,7 +177,7 @@ type LdJsonValue = LdJsonPrimitive | LdJsonObject | LdJsonArray;
/**
* An arbitrary object that is associated with a route.
*/
export type RouteHandle = any;
export type RouteHandle = unknown;
brophdawg11 marked this conversation as resolved.
Show resolved Hide resolved

export interface EntryRouteModule {
ErrorBoundary?: any; // Weakly typed because server-runtime is not React-aware
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"dependencies": {
"@remix-run/node": "2.0.0-pre.4",
"@remix-run/react": "2.0.0-pre.4",
"@remix-run/router": "1.9.0-pre.0",
"react-router-dom": "6.16.0-pre.0"
"@remix-run/router": "1.9.0-pre.1",
"react-router-dom": "6.16.0-pre.1"
},
"devDependencies": {
"@types/node": "^18.17.1",
Expand Down
30 changes: 15 additions & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2132,10 +2132,10 @@
"@changesets/types" "^5.0.0"
dotenv "^8.1.0"

"@remix-run/router@1.9.0-pre.0":
version "1.9.0-pre.0"
resolved "https://registry.npmjs.org/@remix-run/router/-/router-1.9.0-pre.0.tgz#a91357c467c7f171096482241729446dde23a499"
integrity sha512-E0r4uaA2kS3Ks02KLXC2XD7vSlTMQmvfgb7rhaA1ni0O73GHMP38qsR3IjFvlnWNZWhOyzv2rHBtaYBZz2+I1w==
"@remix-run/router@1.9.0-pre.1":
version "1.9.0-pre.1"
resolved "https://registry.npmjs.org/@remix-run/router/-/router-1.9.0-pre.1.tgz#f9d2d60bf0a65d9a29cd78eac90aa556fb40b00b"
integrity sha512-uRNAn+/4nyvoRIXjych6RweUgFua3fXmUa1lTRxReVaRkux33R9aCqfgapQP5RgduAq4K06JRaNLnKBYmRW/0A==

"@remix-run/web-blob@^3.0.5":
version "3.0.5"
Expand Down Expand Up @@ -10128,20 +10128,20 @@ react-refresh@^0.14.0:
resolved "https://registry.npmjs.org/react-refresh/-/react-refresh-0.14.0.tgz"
integrity sha512-wViHqhAd8OHeLS/IRMJjTSDHF3U9eWi62F/MledQGPdJGDhodXJ9PBLNGr6WWL7qlH12Mt3TyTpbS+hGXMjCzQ==

react-router-dom@6.16.0-pre.0:
version "6.16.0-pre.0"
resolved "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.16.0-pre.0.tgz#29b1f11c6352c64a835379724caf6d3884fab819"
integrity sha512-2SiKRx2hxtk97B0eOtX8Oj6Ei8JjTKd02NQRbrHWYi20oJtOOVOYeXIcwq/kDinqkGAKjp2+5q7hDoc35YNJCA==
react-router-dom@6.16.0-pre.1:
version "6.16.0-pre.1"
resolved "https://registry.npmjs.org/react-router-dom/-/react-router-dom-6.16.0-pre.1.tgz#0c7e5e8a3192603b12c754ba47be2c8301ca43c7"
integrity sha512-W6rrNTPaKM/4QTBYE+PElGiHaYo0Kxr+69rbUg6Hs5HzCQYfhH02tree5ZP8pLRGmMAc1KfK31RiBkYwSpXRkw==
dependencies:
"@remix-run/router" "1.9.0-pre.0"
react-router "6.16.0-pre.0"
"@remix-run/router" "1.9.0-pre.1"
react-router "6.16.0-pre.1"

react-router@6.16.0-pre.0:
version "6.16.0-pre.0"
resolved "https://registry.npmjs.org/react-router/-/react-router-6.16.0-pre.0.tgz#85ed4b71e01185349e8fb9e822676d2057995759"
integrity sha512-dvaErR6mV3UVZV9rzAOKaraG6s+Qkl78FnkVTC4MI83ZqKA2jqpF+aIxN8S7JY2Fhw1EcfB5HtvKRlyVQpBt7A==
react-router@6.16.0-pre.1:
version "6.16.0-pre.1"
resolved "https://registry.npmjs.org/react-router/-/react-router-6.16.0-pre.1.tgz#3af6fe265a6d1fecf9321a4c2f60986a5f5137c0"
integrity sha512-kFDaSkiedeNhaq6OgDwilUzppxzeIFmmbv2WQ1YqxD6yk74n20KXGIYGdiipiMr8Qr/5cZ/j9ISj720zeFQinQ==
dependencies:
"@remix-run/router" "1.9.0-pre.0"
"@remix-run/router" "1.9.0-pre.1"

react@^18.2.0:
version "18.2.0"
Expand Down