`babel-plugin-react-css-modules`打字稿类型检查

问题描述 投票:0回答:2

我正在使用babel-plugin-react-css-modules。它将styleName属性添加到HTML元素。 Typescript无法识别此属性。我很确定我需要扩展一些东西,但我尝试过的东西都没有。

我试过在'.d.ts'文件中声明一个Element来做声明合并,但我一定做错了。

interface Element {
    styleName: string;
}

export const Wrapper: React.FC<IWrapperProps> = ({ children, style }) => (
  <div styleName="wrapper" style={style}>
    {children}
  </div>
);

更新:

.d.ts not working

typescript babel babel-plugin-react-css-modules
2个回答
0
投票

由于您在一个模块中(它有一个导出),您正在使用一个与全局范围不同的范围 - 因此您只需创建一个名为Element的接口,该接口与全局声明分开。

我将接口放在一个没有导出/导入(augmentations.d.ts)的单独文件中,并将其添加到tsconfig.json


0
投票

添加@types/react-css-modules到你的package.json和styleName将被识别:

npm install @types/react-css-modules --save-dev
© www.soinside.com 2019 - 2024. All rights reserved.