smastrom / react-rating

⭐ Zero-dependency, highly customizable rating component for React.
https://reactrating.netlify.app
MIT License
337 stars 6 forks source link

Type declaration file not found in Next.js app with Typescript #11

Closed alicia-lyu closed 1 year ago

alicia-lyu commented 1 year ago

The following static checking error pops up with import { Rating } from "@smastrom/react-rating".

Could not find a declaration file for module '@smastrom/react-rating'. '/path/to/node_modules/@smastrom/react-rating/dist/index.mjs' implicitly has an 'any' type.
There are types at '/path/to/node_modules/@smastrom/react-rating/dist/index.d.ts', but this result could not be resolved when respecting package.json "exports". The '@smastrom/react-rating' library may need to update its package.json or typings.ts(7016)

Environment

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "typeRoots": ["./node_modules/@types", "./node_modules"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

My temporary solution

I added a new file react-rating.d.ts in the root directory with the following code:

declare module '@smastrom/react-rating';

The error is resolved.

alicia-lyu commented 1 year ago

There was a mistake with my tsconfig.json. typeRoots should be ["./node_modules/@types", "./node_modules/*"]. However, this doesn't fix the static checking problem.

I should also mention that even with the temporary fix, there is not auto completion with Rating in my IDE.

smastrom commented 1 year ago

Hi @alicia-lyu, sorry for getting back to you so late. Yes, there was indeed an issue with react-rating package.json exports field which affected NextJS 13.

I just released a new version that fixes it and adds support for the app router.

Thank you for reporting the issue.

alicia-lyu commented 1 year ago

Hi @alicia-lyu, sorry for getting back to you so late. Yes, there was indeed an issue with react-rating package.json exports field which affected NextJS 13.

I just released a new version that fixes it and adds support for the app router.

Thank you for reporting the issue.

Don't worry! Thank you for the new release!