I have a utility function in my codebase that helps parse URL query parameters, and it is located within my `utils` package. Here is the code snippet for the function:
export function urlQueryParamParser(params: URLSearchParams) {
const output:any = {};
const searchParams = new URLSearchParams(params);
new Set([...searchParams.keys()])
.forEach(key => {
output[key] = searchParams.getAll(key).length > 1 ?
searchParams.getAll(key) :
searchParams.get(key)
});
return output;
}
To make this function accessible throughout my project, I export it in the main index file along with other modules like so:
export * from './utils/urlQueryParamParser'
When I import and use this function in one of my modules using the statement
import { urlQueryParamParser } from '@mypackages/utils'
, I notice that the result returned is not as expected:
{
"[object Iterator]": null
}
However, if I directly copy this function into the module where I am calling it, the return result is correct and looks something like this:
{
"filter": "all",
"query": "",
"range": "today",
"from": "2022-11-22",
"to": "2022-12-22",
"claims": [
"damaged",
"missing-article"
]
}
I am curious about why there is a difference in the results when importing the function versus using it directly within the same file. The TypeScript version used in the module where I import the function is:
"typescript": "^4.7.2",
And the `tsconfig.json` configuration for that module is as follows:
{
"compilerOptions": {
// Configuration options here...
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"files": ["custom.d.ts"]
}
On the other hand, the TypeScript version specified in the package module is:
"typescript": "^4.7.4"
And its corresponding `tsconfig.json` content is provided below:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
// Configuration options here...
},
"exclude": ["**/node_modules", "**/.*/", "dist", "build"],
"include": ["src/**/*.ts", "src/**/*.tsx"]
}