Next.js Discord

Discord Forum

Pattern to convert all params from string to number?

Unanswered
Sphecid wasp posted this in #help-forum
Open in Discord
Sphecid waspOP
Hi, I've got a page with 3 page params. All are numbers as strings. I wanted to convert all values of the params object from the useParams hook to numbers but I'm running into some issues. The cleanest solution I've found so far is:
type IrrigationClientPageParams = {
  irrigationClientId: string;
  irrigationSourceId: string;
  irrigationYearId: string;
};

const params = useParams<IrrigationClientPageParams>();

const irrigationClientId = parseInt(params.irrigationClientId);
const irrigationSourceId = parseInt(params.irrigationSourceId);
const irrigationYearId = parseInt(params.irrigationYearId);

But I was trying stuff like this:
let queryParams;
for (let k in params) {
  queryParams[k] = parseInt(params[k]);
}
useQuery(queryParams);

This works alright in JavaScript but TypeScript has a lot of baggage and it ends up being a nightmare. Any thoughts or experiences with the params router?

0 Replies