Next.js' API Routes receive a req object - it's an extension of http.IncomingMessage with additional middlewares such as req.query. The typing of req.query, found in their utils.ts, is:
query: {
[key: string]: string | string[]
}
Why is it possible to receive an array of strings from the query?
I'm trying to perform string methods on my query values but run into TS errors -_-
someString.split() // => Property 'split' does not exist on type 'string | string[]'.
I think we can use this, if we don't work with more than one param with the same name:
const id = req.query.id as string
You can convert 'string | string[]' to 'string' by joining them like this:
let { param } = req.query.param;
if (Array.isArray(param)) {
param = param.join('');
}