Skip to content

Commit

Permalink
Merge pull request #100 from zoontek/fix-use-query-params-regression
Browse files Browse the repository at this point in the history
Fix useQueryParams typing regression
  • Loading branch information
kyeotic authored Aug 17, 2021
2 parents 6968297 + 6bef70d commit 0d352ee
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/querystring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export interface setQueryParamsOptions {
replace?: boolean
}

export function useQueryParams(
parseFn: (query: string) => QueryParam = parseQuery,
serializeFn: (query: QueryParam) => string = serializeQuery
): [QueryParam, (query: QueryParam, options?: setQueryParamsOptions) => void] {
export function useQueryParams<T extends QueryParam>(
parseFn: (query: string) => T = parseQuery,
serializeFn: (query: T) => string = serializeQuery
): [T, (query: T, options?: setQueryParamsOptions) => void] {
const [querystring, setQuerystring] = useState(getQueryString())
const setQueryParams = useCallback(
(params, { replace = true } = {}) => {
Expand All @@ -38,12 +38,12 @@ export function useQueryParams(
return [parseFn(querystring), setQueryParams]
}

function parseQuery(querystring: string) {
function parseQuery<T extends QueryParam>(querystring: string): T {
const q = new URLSearchParams(querystring)
return Object.fromEntries(q.entries())
return Object.fromEntries(q.entries()) as T
}

function serializeQuery(queryParams: QueryParam): string {
function serializeQuery<T extends QueryParam>(queryParams: T): string {
return new URLSearchParams(
Object.entries(queryParams).filter(([, v]) => v !== null)
).toString()
Expand Down

0 comments on commit 0d352ee

Please sign in to comment.