Reactjs 如何导入和使用存储在自动生成的*.d.ts文件中的typescript接口?
在React应用程序中,我得到了以下结构:Reactjs 如何导入和使用存储在自动生成的*.d.ts文件中的typescript接口?,reactjs,typescript,typescript-typings,Reactjs,Typescript,Typescript Typings,在React应用程序中,我得到了以下结构: src/Components/MyComponent ├── MyComponent.module.css // Css modules styles ├── MyComponent.module.css.d.ts // Autogenerated style type definition └── MyComponent.tsx // The React component Component.module.css.d.ts包含以下内容: in
src/Components/MyComponent
├── MyComponent.module.css // Css modules styles
├── MyComponent.module.css.d.ts // Autogenerated style type definition
└── MyComponent.tsx // The React component
Component.module.css.d.ts
包含以下内容:
interface CssExports {
'myCssClass01': string;
'myCssClass02': string;
}
declare const cssExports: CssExports;
export = cssExports;
在MyComponent.tsx
中,我想导入接口
并在函数中使用它,如下所示:
import styles from './MyComponent.module.css'
import { cssExports } from './MyComponent.module.css.d'
const myFunc = (style: cssExports): string => {...}
我收到错误信息:
模块'/src/components/MyComponent/MyComponent.Module.css''没有导出的成员'cssExports'。ts(2305)
我哪里做错了?看起来您使用的是commonjs模块语法,能否在tsconfig.json中启用此标志并重试
"esModuleInterop": true
也可以像下面一样导出接口
//MyComponent.module.css
谢谢,
esModuleInterop
已经是true
MyComponent.module.css.d.ts
由css模块类型脚本加载程序生成。如果我可以编辑它,直接在接口上添加导出而不是默认导出,我可能可以通过以下方式解决导入接口的问题:import style,{cssExports}from./MyComponent.module.css'
,但我无法-(
export interface CssExports {
'myCssClass01': string;
'myCssClass02': string;
}