简体   繁体   English

反应 CSS 模块导入与 typescript 不兼容

[英]React CSS modules import incopatible with typescript

I want to use css modules in my react application.我想在我的反应应用程序中使用 css 模块。 I installed 'typings-for-css-modules-loader' nuget package and configure in webpack.config.js like this我安装了'typings-for-css-modules-loader' nuget package 并像这样在 webpack.config.js 中配置

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

file styles.css.d.ts is generated and looks like this:生成文件 styles.css.d.ts,如下所示:

export interface IStylesCss {
  x: string;
}

export const locals: IStylesCss;
export default locals;

i am using awesome-typescript-loader which is configured in webpack.config.js as follows:我正在使用在 webpack.config.js 中配置的 awesome-typescript-loader,如下所示:

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

Here is a problem description:下面是一个问题描述:

If i use import * as styles from './styles.css';如果我使用import * as styles from './styles.css'; + className={styles.x} not working due to compilation error 'Property 'x' does not exist on type...' + className={styles.x}由于编译错误“类型上不存在属性 'x'...”而无法工作

className={styles.default.x} do not work either because default is undefined. className={styles.default.x}也不起作用,因为默认值未定义。

default import import styles from './styles.css';默认导入import styles from './styles.css'; + className={styles.x} not working, again, a default is undefined. + className={styles.x}不起作用,同样,默认值未定义。

import * as styles from './styles.css'; + className={(styles as any).x} is working but i loose strong typing and it doesnt look correct solution to me. + className={(styles as any).x}正在工作,但我松散了强类型,它看起来对我来说不是正确的解决方案。

Setting allowSyntheticDefaultImports and esModuleInterop to true in tsconfig.json asi hotfix worked for me.在 tsconfig.json asi 修补程序中将 allowSyntheticDefaultImports 和 esModuleInterop 设置为 true 对我有用。 Waiting for fix.等待修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM