Is it possible to restrict the req.query
in NextJS NextApiRequest
to only accept string
types instead of string | string[]
? For instance, if someQueryParam
is currently of type string | string[]
, but I need it to be just a string
.
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Response>
) {
const { someQueryParam } = req.query;
// Passing someQueryParam as a string into a function call
const result = functionCall(someQueryParam)
}
When attempting to pass someQueryParam
into a function that only accepts string
, TypeScript throws an error:
Argument of type 'string | string[]' is not assignable to parameter of type 'string'.
Type 'string[]' is not assignable to type 'string'.
Here is the definition of NextApiRequest
in the Next.js library for reference:
https://i.sstatic.net/s9Lud.png
I am also open to suggestions on how to utilize someQueryParam
as a string
type within the handler before passing it into the function.