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

Conversation

lomirus
Copy link

@lomirus lomirus commented Oct 29, 2021

Hello, I am using Deno to try to write a chatroom demo, and now I think I've encountered a problem. Here's an example code:

const username = helpers.getQuery(context).username;

Here the VSCode just infers that the type of username is just string and wouldn't be undefined because the type of the return value of getQuery() is Record<string, string>. But in fact it sometimes does occur. And it also made me ignore considering the situation that the username would be empty so I didn't write the relevant code to check and avoid it. It may make debug hard so I just modified the type of the return value.

@CLAassistant
Copy link

CLAassistant commented Oct 29, 2021

CLA assistant check
All committers have signed the CLA.

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants