const sortString = req.query.sort as string
const params = Object.fromEntries(new URLSearchParams(sortString))
Upon moving to the implementation phase, I encountered:
declare var URLSearchParams: {
prototype: URLSearchParams;
new(init?: string[][] | Record<string, string> | string | URLSearchParams): URLSearchParams;
toString(): string;
};
It's puzzling why it isn't functioning properly in Typescript despite accepting a string.
How to convert URL parameters to a JavaScript object?
No overload matches this call.
Overload 1 of 2, '(entries: Iterable<readonly [PropertyKey, any]>): { [k: string]: any; }', gave the following error.
Argument of type 'URLSearchParams' is not assignable to parameter of type 'Iterable<readonly [PropertyKey, any]>'.
Property '[Symbol.iterator]' is missing in type 'URLSearchParams' but required in type 'Iterable<readonly [PropertyKey, any]>'.
Overload 2 of 2, '(entries: Iterable<readonly any[]>): any', gave the following error.
Argument of type 'URLSearchParams' is not assignable to parameter of type 'Iterable<readonly any[]>'.
Property '[Symbol.iterator]' is missing in type 'URLSearchParams' but required in type 'Iterable<readonly any[]>'.ts(2769)