'TypeScript: Function Overloading, Objects Using Bracket Notation and Recursion

I have been breaking my head over this for a few hours. How can I type this correctly. The purpose of this function is to recursively flatten an object as demonstrated on this stackOverflow thread. Below I have attached a text copy as well as a screenshot of the code in question and the typescript error for your viewing pleasure. Thanks in advance!

    private flattenObject = (originalJson: object): object => {
        return Object.assign(
          {},
          ...(function flatten(obj: object): Array<object> {
            return [].concat(
              ...Object.keys(obj).map((key: string) =>
                typeof obj[key] === 'object'
                  ? flatten(obj[key])
                  : { [key]: obj[key] },
              ),
            );
          })(originalJson),
        );
      };

screenshot of code above

screenshot of typescript error

ps. I do realize a Json can either be an array or an object but ive tried both and any and still get errors. I'm open to any suggestions at this point.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source