Type error: Property 'getJSON' does not exist on type 'typeof import("js-cookie")'

166 Views Asked by At

I'm unable to build the following JS react code:

import Cookies from 'js-cookie';

React.useEffect(() => {
  
  interface UserInfo {
  name: string;
  verified: boolean;
}
  
  const userInfo = Cookies.getJSON('userInfo');
  if (userInfo && !userInfo.verified) {
    setUserName(`unknown`);
  } else {
    setUserName('else');
  }
}, []);

First, I tried npm install --save @types/js-cookie When I kept getting the error, I created js-cookie.d.ts with this content:

declare module 'js-cookie' {
  interface CookiesStatic<T extends string | undefined = undefined> {
    getJSON(key: string): T extends undefined ? any : T;
  }
}

and placed it under /app/node_modules/@types/js-cookie

Then I also created types folder and place that file there:

/app/src/types/js-cookie.d.ts

And I edited /app/tsconfig.ts

And added the lines:

"types": ["node", "react", "@types/js-cookie"], "typeRoots": ["node_modules/@types", "./src/types"]

nothing worked, still getting same error. how to debug?

0

There are 0 best solutions below