首页 > 解决方案 > 在 index.ts 中找不到导出 typescript 和 javascript 组件的声明文件

问题描述

我开始将设计系统组件转换为 Typescript,但正在努力设置我们的 index.ts 文件。

我不断得到Could not find a declaration file for module xxx. Implicitly has 'any' type

我们当前的文件结构是:

- index.ts
- components
   - Button.tsx
   - Input.jsx
   - Icon.jsx

index.ts 文件如下所示:

export * from './components/Button' // no error
export * from './components/Input' // cannot find declaration
export * from './components/Icon' // cannot find declaration

请注意,我已经将 Button 转换为 .tsx,但直到将来某个时候才能访问其他组件。

我已经.d.ts为设计系统组件创建了一个全局文件,并且可以从应用程序的其他部分导入它们而不会出错,但是 index.ts 文件会发出声明文件错误。有任何想法吗?

标签: typescriptinstallationtype-declaration

解决方案


取决于您的 tsconfig.json 但通常,您需要将非打字稿文件类型声明为模块。例子:

declare module "*.jsx" { }

.jsx文件的特定情况下,您可以指定--jsxFactory选项。在您的 tsconfig.json 中:

{
    "compilerOptions": {
        // Add the following:
        "allowJs": true,
        "jsx": "react",
    },
}

另请参阅Typescript 文档 - JSX

配置 JSX 有多个编译器标志可用于自定义 JSX,它们既可以作为编译器标志,也可以通过内联的每个文件编译指示工作。要了解更多信息,请参阅他们的 tsconfig 参考页面:


推荐阅读