GetStream / react-activity-feed

Stream React Activity Feed Components
https://getstream.io/react-activity-feed/
BSD 3-Clause "New" or "Revised" License
137 stars 83 forks source link

bug: Typescript check ERROR in node_modules/getstream/src/utils.ts:103:15 #383

Closed nima-lighthouse closed 1 year ago

nima-lighthouse commented 1 year ago

Typecheck doesn't pass on the library init. Cannot start running the app

ERROR in node_modules/getstream/src/utils.ts:103:15
TS2769: No overload matches this call.
  Overload 1 of 2, '(o: {}): string[]', gave the following error.
    Argument of type 'T' is not assignable to parameter of type '{}'.
  Overload 2 of 2, '(o: object): string[]', gave the following error.
    Argument of type 'T' is not assignable to parameter of type 'object'.
    101 |   const cloned = {};
    102 |
  > 103 |   Object.keys(obj).forEach((k) => {
        |               ^^^
    104 |     // @ts-expect-error
    105 |     cloned[k] = replaceStreamObjects(obj[k]);
    106 |   });
**To Reproduce**

Version of React and TS "react": "^18.0.0", "typescript": "^4.9.4", "@typescript-eslint/eslint-plugin": "^5.48.0", "@typescript-eslint/parser": "^5.40.0",

Simple fix solution: In getstream/src/utils.ts

// TODO: refactor and add proper types
function replaceStreamObjects<T, V>(obj: T): V {
  // @ts-expect-error
  if (Array.isArray(obj)) return obj.map((v) => replaceStreamObjects(v));

  // @ts-expect-error
  if (Object.prototype.toString.call(obj) !== '[object Object]') return obj;

  // @ts-expect-error
  if (typeof obj.ref === 'function') return obj.ref();

  const cloned = {};

  // @ts-expect-error <--------- ADDITIONAL COMMENT TO FIX THE ERROR
  Object.keys(obj).forEach((k) => {
    // @ts-expect-error
    cloned[k] = replaceStreamObjects(obj[k]);
  });

  // @ts-expect-error
  return cloned;
}