【问题标题】:React CSS modules import incopatible with typescriptReact CSS 模块导入与打字稿不兼容
【发布时间】:2020-03-03 20:12:51
【问题描述】:

我想在我的 react 应用程序中使用 css 模块。我安装了 'typings-for-css-modules-loader' nuget 包并像这样在 webpack.config.js 中配置

test: /\.css$/,
use: [
    'style-loader',
    '@teamsupercell/typings-for-css-modules-loader',
    {
        loader: "css-loader",
        options: {
            modules: true,
        }
    }
]

文件styles.css.d.ts 生成后如下所示:

export interface IStylesCss {
  x: string;
}

export const locals: IStylesCss;
export default locals;

我正在使用在 webpack.config.js 中配置的 awesome-typescript-loader,如下所示:

{
    test: /\.tsx?$/,
    use:
    {
        loader: "awesome-typescript-loader"
    }
},

这是一个问题描述:

如果我使用 import * as styles from './styles.css'; + className={styles.x} 由于编译错误“类型上不存在属性“x”而无法工作...”

className={styles.default.x} 也不起作用,因为默认值未定义。

默认导入 import styles from './styles.css'; + className={styles.x} 不起作用,再次声明默认值未定义。

import * as styles from './styles.css'; + className={(styles as any).x} 正在工作,但我松散了强类型,对我来说它看起来不是正确的解决方案。

【问题讨论】:

  • 同样的问题,有更新吗?

标签: reactjs typescript webpack css-modules


【解决方案1】:

在 tsconfig.json 中将 allowSyntheticDefaultImports 和 esModuleInterop 设置为 true,因为热修复对我有用。等待修复。

【讨论】:

    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 2017-10-29
    • 2022-01-14
    • 2018-01-10
    • 1970-01-01
    相关资源
    最近更新 更多