20

Don't know why typescript suddenly not recogize my code, it show

Cannot find module './hookStyle/useMemostyle.module.css' or its corresponding type declarations

It happen when i move style to another folder

Here is typescript config

{
  "compilerOptions": {
    "lib": [
      "ESNext",
      "dom"
    ],
    "outDir": "lib",
    "removeComments": true,
    "target": "ES6",
    "baseUrl": "src",
    "esModuleInterop": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "sourceRoot": "/",
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noImplicitReturns": true,
    "noUncheckedIndexedAccess": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "allowJs": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "./**/*.ts"
  ],
  "exclude": [
    "node_modules/**/*"
  ]
}

It not effect the code, it still working but why typescript show that error? How can i fix it?

2 Answers 2

52

To import custom file types, you must use TypeScript's declare module syntax to let it know that it's ok to import. To do this, simply create a globals.d.ts (declaration file) wherever your other code's root directory is and then add in this code:

declare module '*.css';

You may also need to add the path to "globals.d.ts" into the "include" property in your tsconfig.json file if this still does not work (Thanks to @Ash Archin for this advice).

Sign up to request clarification or add additional context in comments.

6 Comments

For me this file should also add as "globals.d.ts" in the include property in tsconfig.json
Put the 'globals.d.ts' into the src folder of your project.
If it does not help, check if globals.d.ts is included in tsconfig.json & try removing imports in globals.d.ts. See also stackoverflow.com/questions/40382842/…
@AshArchin thats probably a very important piece of this pie. Can we add that on the answer solution statement above... ?
I added Ash's advice to the answer.
|
15

An alternative solution that worked for me was to install an additional TypeScript types dependency; @types/css-modules, to automatically add type-safe import definitions for the following style modules:

  • *.css
  • *.scss
  • *.sass
  • *.less
  • *.styl

The list above may become out-of-date over time, and so you may view the latest supported type-safe style module imports.

That was all that was needed to resolve the TS lint error for being unable to find the CSS module, without needing to having to create any declaration file (e.g. *.d.ts).

Here is the command you may like to try and use to install the dependency locally to your project:

npm install --save-dev @types/css-modules

💡 If after checking over the latest up-to-date style modules that are currently supported by the dependency, and you feel it is legitimately missing a declaration for particular a style module, then feel free to contribute to and extend the @types/css-modules type dependency project, which is maintained within the open-source DefinitelyTyped GitHub repository.

2 Comments

This should be the way to go since declaring module individually becomes unmaintainable over time.
This worked well for me in an esbuild project - .d.ts files seem to be a lot more annoying to use there, due to the lack of tsconfig.json file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.