2

我尝试在 typescript 中使用 react-css-modules,但我收到一条错误消息,无法将 styleName 添加到 div 元素。这是代码。

import * as Immutable from 'immutable';
import * as React from 'react';
import * as  CSSModules from 'react-css-modules';
import { connect } from 'react-redux';
import { bindActionCreators, Dispatch } from 'redux';
const styles = require<any>('./style.scss');

interface RootProps {
    data: Immutable.List<string>;
    dispatch: Dispatch<any>;
}

@CSSModules(styles)
export class Root extends React.Component<RootProps, {}>{
    render() {
        return (
            <div styleName='root'>
              Hello
            </div>
        )
    }
}


export default connect(mapStateToProps, mapDispatchToProps)(Root);

我收到此错误消息:'Property 'styleName' does not exist on type 'HTMLProps<HTMLDivElement>'

和 tsconfig

"compilerOptions": {
    "target": "es5",
    "moduleResolution": "node",
    "jsx": "react",
    "experimentalDecorators": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "allowSyntheticDefaultImports": true,
    "allowJs": true,
    "sourceMap": true,
}
4

3 回答 3

7

使用@types/react-css-modules。

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

或者,

yarn add @types/react-css-modules --dev
于 2017-09-28T11:47:55.370 回答
0

如果有人在使用时偶然发现相同的错误,请在 /types 目录中preact添加preact.d.ts文件,其中包含以下内容:

import preact from 'preact';

declare global {
  namespace JSX {
    interface HTMLAttributes {
      styleName?: string;
    }

    interface SVGAttributes {
      styleName?: string;
    }
  }
}
于 2019-04-08T15:52:17.553 回答
0

我使用 VSCode IDE 我只是更改了工作空间的类型脚本版本而不是 IDE 版本。

于 2018-10-17T06:04:20.913 回答