How Can I Correctly Stringify and Parse an Object with a Single-Item Array Using the `qs` Library?

I have an object with properties such as string, boolean, and array, and I want to convert it to a query string.

Using the qs library, I can stringify the array with comma separation (an option available in qs), and this works correctly.

However, the issue arises when I try to parse back the query string generated by stringify. The query string turns into foo=true&arr=1, and the parse function interprets it as another property, resulting in arr: 1.

This problem occurs only when there is a single item in the array along with other properties (like foo).

Here’s the code:

StackBlitz Link

import qs from 'qs';

console.clear();

const str = qs.stringify({ foo: true, arr: [1] }, { arrayFormat: 'comma' });

const parsed = qs.parse(str);

console.log(parsed);