简体   繁体   English

如何导入和使用存储在自动生成的 *.d.ts 文件中的 typescript 接口?

[英]How to import and use typescript interface stored in a auto generated *.d.ts file?

In a React Application, I have got this structure:在 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 contains this: Component.module.css.d.ts包含以下内容:

interface CssExports {
  'myCssClass01': string;
  'myCssClass02': string;
}
declare const cssExports: CssExports;
export = cssExports;

In MyComponent.tsx I want to import the interface and use it in a function, like this:MyComponent.tsx中,我想导入interface并在 function 中使用它,如下所示:

import styles from './MyComponent.module.css'
import { cssExports } from './MyComponent.module.css.d'

const myFunc = (style: cssExports): string => {...}

I am receiving error:我收到错误:

Module '"/src/components/MyComponent/MyComponent.module.css"' has no exported member 'cssExports'.ts(2305)模块 '"/src/components/MyComponent/MyComponent.module.css"' 没有导出成员 'cssExports'.ts(2305)

Where am I doing wrong?我在哪里做错了?

Looks like you are using commonjs module syntax, Can you enable this flag in tsconfig.json and try again看起来您正在使用 commonjs 模块语法,您可以在 tsconfig.json 中启用此标志并重试

"esModuleInterop": true 

Also export interface as well like below也导出界面,如下所示

// MyComponent.module.css // MyComponent.module.css

export interface CssExports {
  'myCssClass01': string;
  'myCssClass02': string;
}

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

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