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

fix: getQuery() may have undefined values #423

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
8 changes: 4 additions & 4 deletions helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ export type GetParamsOptions = GetQueryOptionsBase | GetQueryOptionsAsMap;
export function getQuery(
ctx: Context | RouterContext,
options: GetQueryOptionsAsMap,
): Map<string, string>;
): Map<string, string | undefined>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary, as Map is possibly undefined in strict mode already.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if we update the type of result to Record<string, string | undefined>, then the new Map(Object.entries(result)) will also return a value of Map<string, string | undefined>, which is not assignable to Map<string, string>.

/** Given a context, return the `.request.url.searchParams` as a record object
* of keys and values of the params. */
export function getQuery(
ctx: Context | RouterContext,
options?: GetQueryOptionsBase,
): Record<string, string>;
): Record<string, string | undefined>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only thing that needs possibly undefined.

export function getQuery(
ctx: Context | RouterContext,
{ mergeParams, asMap }: GetParamsOptions = {},
): Map<string, string> | Record<string, string> {
const result: Record<string, string> = {};
): Map<string, string | undefined> | Record<string, string | undefined> {
const result: Record<string, string | undefined> = {};
if (mergeParams && isRouterContext(ctx)) {
Object.assign(result, ctx.params);
}
Expand Down