Infer type of searchParams #571
Answered
by
franky47
missile-developer
asked this question in
Q&A
-
I want to refer my fetcher query params, In Zod you can use z.infer. How about nuqs? |
Beta Was this translation helpful? Give feedback.
Answered by
franky47
Aug 30, 2024
Replies: 1 comment 7 replies
-
Assuming you have an object describing your search params as such: import { parseAsInteger, parseAsBoolean, parseAsString } from 'nuqs'
const searchParams = {
int: parseAsInteger,
bool: parseAsBoolean,
stringWithDefault: parseAsString.withDefault('')
}
// And you want the following type as output?
type inferred = {
int: number | null
bool: boolean | null
stringWithDefault: string
} There is a (contrived) way to do this currently, but it could definitely use a little abstraction, I'll have a look at what I can do. import type { createSearchParamsCache } from 'nuqs/server'
type inferred = ReturnType<
ReturnType<typeof createSearchParamsCache<typeof searchParams>>['parse']
> |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shipped in version 1.18.0 🚀
https://nuqs.47ng.com/docs/utilities#parser-type-inference