Self referencing function in TypeScript?

I'm wondering how to define the type for the defaultEncoder parameter. It has the same parameters as the function itself.

const paramsSerializer = (params: Params) => {
  const encoder = (
    str: string,
    defaultEncoder: any,
    charset: string,
    type: string
  ) => {
    const encodedStr = defaultEncoder(str, defaultEncoder, charset, type);
    return transformEncodedStr(encodedStr)
  };

  return transformParams(params, { encoder });
}

If I define the defaultEncoder every time it would be an infinite sequence of types.

defaultEncoder: (
  str: string,
  encoder: (
    str: string,
    encoder: // And so on...
    charset: string,
    type: "key" | "value"
  ) => any,
  charset: string,
  type: "key" | "value"
) => any,


Read more here: https://stackoverflow.com/questions/67007985/self-referencing-function-in-typescript

Content Attribution

This content was originally published by Nepo Znat at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: