-
Notifications
You must be signed in to change notification settings - Fork 233
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
base: main
Are you sure you want to change the base?
Conversation
@@ -25,18 +25,18 @@ export type GetParamsOptions = GetQueryOptionsBase | GetQueryOptionsAsMap; | |||
export function getQuery( | |||
ctx: Context | RouterContext, | |||
options: GetQueryOptionsAsMap, | |||
): Map<string, string>; | |||
): Map<string, string | undefined>; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>; |
There was a problem hiding this comment.
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.
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:
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()
isRecord<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.