From 3b37f72b3bbde7f3f768b93245e6193b99889ce5 Mon Sep 17 00:00:00 2001 From: Matt Brophy Date: Fri, 24 Mar 2023 16:06:12 -0400 Subject: [PATCH] Disallow undefined as a return value from Loaderfunction/ActionFunction --- .changeset/disallow-return-undefined-type.md | 5 +++++ packages/router/utils.ts | 11 +++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 .changeset/disallow-return-undefined-type.md diff --git a/.changeset/disallow-return-undefined-type.md b/.changeset/disallow-return-undefined-type.md new file mode 100644 index 0000000000..b6c446a2dc --- /dev/null +++ b/.changeset/disallow-return-undefined-type.md @@ -0,0 +1,5 @@ +--- +"@remix-run/router": patch +--- + +Enhance `LoaderFunction`/`ActionFunction` return type to prevent `undefined` from being a valid return value diff --git a/packages/router/utils.ts b/packages/router/utils.ts index 4851168d21..777ef3fae4 100644 --- a/packages/router/utils.ts +++ b/packages/router/utils.ts @@ -123,18 +123,25 @@ export interface LoaderFunctionArgs extends DataFunctionArgs {} */ export interface ActionFunctionArgs extends DataFunctionArgs {} +/** + * Loaders and actions can return anything except `undefined` (`null` is a + * valid return value if there is no data to return). Responses are preferred + * and will ease any future migration to Remix + */ +type DataFunctionValue = Response | NonNullable | null; + /** * Route loader function signature */ export interface LoaderFunction { - (args: LoaderFunctionArgs): Promise | Response | Promise | any; + (args: LoaderFunctionArgs): Promise | DataFunctionValue; } /** * Route action function signature */ export interface ActionFunction { - (args: ActionFunctionArgs): Promise | Response | Promise | any; + (args: ActionFunctionArgs): Promise | DataFunctionValue; } /**