ARTICLE AD BOX
i'm trying to have a convenience method that works with both query string providers in nextjs.
when you are in the page.tsx context, you get an object with string keys and values.
When you are in the route.tsx context, you get a proper URLSearchParams.
I tried the accepted type match pattern but it doesn't seem to work in real life: (the signature for qs is dictated by nextjs)
/** * @param qs the provider you get from nextjs * @param key the key string to get value from * @return a single string or null. if multiple values, only the first is seen **/ export function get_query_string_as_string( qs :URLSearchParams|{[key :string] :string|string[]|undefined}, key :string) :null|string { // should be easy to check if there's a .get() or else call check if there's the bare key... const val = (_isUrlSearchParams(qs)) ? qs.get(key) : qs[key]; ... function _isUrlSearchParams(x :URLSearchParams|object){ return (<URLSearchParams>x).get !== undefined; }i could only placate the compiler with
let val :string|string[]|null|undefined; if( qs && qs.get && qs.get !== undefined && typeof qs.get === 'function' ){ val = qs.get(key); }else if( qs && typeof qs === 'object' && qs instanceof URLSearchParams === false ){ val = qs[key]; }which feels kinda insane and look like slop ai generate nonsense. What am i missing from the recommended pattern in this case?
