【问题标题】:ReactJS: Cannot find module './App.module.css' or its corresponding type declarationsReactJS:找不到模块'./App.module.css'或其相应的类型声明
【发布时间】:2021-12-26 10:03:24
【问题描述】:

我今天打开我的旧项目,看到这些奇怪的警告:

ERROR in src/App.tsx:13:17
TS2307: Cannot find module './App.module.css' or its corresponding type declarations.
    11 | import { Layout, Breadcrumb } from 'antd'
    12 | import Header from './components/Header/Header'
  > 13 | import css from './App.module.css'
       |                 ^^^^^^^^^^^^^^^^^^
    14 | import { ROUTES } from './constants/routes'
    15 | import Menu from './components/Menu'
    16 |

ERROR in src/components/common/FormsControls/FormsControls.tsx:2:17
TS2307: Cannot find module './FormsControls.module.css' or its corresponding type declarations.
    1 | import { FC, MouseEvent } from 'react'
  > 2 | import css from './FormsControls.module.css'
      |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    3 | import commonCss from '../styles.module.css'
    4 | import { Field, WrappedFieldProps } from 'redux-form'
    5 | import cn from 'classnames'

其中还有很多文件格式为 svg、png 等。我所有的软件包现在都是最新版本。 以前没有这样的问题,有没有人可以帮忙解决这个问题,不用纠结webpack?

【问题讨论】:

    标签: reactjs react-create-app


    【解决方案1】:

    创建一个名为 declarations.d.ts 的文件(您可以随意命名)
    有时必须重新加载 IDE,但并非总是如此。

    // declarations.d.ts
    declare module '*.css';
    declare module '*.scss';
    declare module '*.svg';
    // etc ...
    

    TypeScript 不知道除了 .ts 或 .tsx 之外的文件,因此如果导入的文件后缀未知,它会报错,所以在这种情况下,我们明确告诉编译器我们的意思是加载一个可以加载的模块有以下扩展名。

    【讨论】:

    • 我把这个文件放在项目中并重启文件夹,但我仍然在控制台看到错误
    • 我可以看看你的文件夹结构吗?
    猜你喜欢
    • 2022-12-16
    • 2023-01-31
    • 2020-10-19
    • 2021-09-21
    • 2020-12-23
    • 2020-08-16
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多