Stack Exchange
Type 'string[]' is not assignable to type 'never[]'. typescript
I have code like this:
export const combineUrlParams = (url = "", params: object) => {
  const keys = Object.keys(params);
  const paramUrl = keys
   .reduce(
     (result, key) =>
       params[key] !== undefined && params[key] !== null && params[key] !== ""
       ? [...result, `${key}=${params[key]}`]
       : [...result],
       []
   )
   .join("&");
  return `${url}?${paramUrl}`;
};

And it got error like below:
Type 'string[]' is not assignable to type 'never[]

Property 'join' does not exist on type 'string'

I copy it from javasript to typescript. Could someone help me to fix this ?

[] is an empty array, so typescript can't infer what kind of values are allowed inside it.
You can be explicit:
([] as string[])


That said, your function has a bunch of other problems (like not escaping special characters in the data) and you are reinventing a wheel that is built into browsers.


const url = new URL("https://example.com");
const data = { foo: "bar", baz: 123, needsEscaping: "1&2=3" };
url.search = new URLSearchParams(data);
console.log(url.toString());



