【问题标题】:Cannot find a declaration file in index.ts that exports typescript and javascript components在 index.ts 中找不到导出 typescript 和 javascript 组件的声明文件
【发布时间】:2021-08-24 19:23:33
【问题描述】:

我开始将设计系统组件转换为 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 文件会发出声明文件错误。有什么想法吗?

【问题讨论】:

    标签: typescript installation type-declaration


    【解决方案1】:

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

    declare module "*.jsx" { }
    

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

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

    另请参阅Typescript Documentation - JSX

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

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 2020-08-02
      • 2020-11-28
      • 1970-01-01
      • 2017-02-16
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      相关资源
      最近更新 更多