找不到模块“module.css”或其相应的类型声明

Lê *_*ánh 15 javascript typescript

不知道为什么打字稿突然无法识别我的代码,它显示

Cannot find module './hookStyle/useMemostyle.module.css' or its corresponding type declarations
Run Code Online (Sandbox Code Playgroud)

当我将样式移动到另一个文件夹时会发生这种情况

这是打字稿配置

{
  "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/**/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

它不会影响代码,它仍然有效,但为什么打字稿显示该错误?我该如何修复它?

sno*_*no2 39

要导入自定义文件类型,您必须使用TypeScript 的declare module语法来让它知道可以导入。为此,只需globals.d.ts在其他代码的根目录所在的位置创建一个(声明文件),然后添加以下代码:

declare module '*.css';
Run Code Online (Sandbox Code Playgroud)

如果这仍然不起作用,您可能还需要将路径添加"globals.d.ts"到文件"include"中的属性中(感谢@Ash Archin 提供的建议)。tsconfig.json

  • 对我来说,这个文件还应该在 tsconfig.json 的 include 属性中添加为“globals.d.ts” (11认同)
  • 将“globals.d.ts”放入项目的 src 文件夹中。 (7认同)