typescript - 在 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 文件会发出声明文件错误。有任何想法吗?
解决方案
取决于您的 tsconfig.json 但通常,您需要将非打字稿文件类型声明为模块。例子:
declare module "*.jsx" { }
在.jsx
文件的特定情况下,您可以指定--jsxFactory
选项。在您的 tsconfig.json 中:
{
"compilerOptions": {
// Add the following:
"allowJs": true,
"jsx": "react",
},
}
另请参阅Typescript 文档 - JSX。
配置 JSX 有多个编译器标志可用于自定义 JSX,它们既可以作为编译器标志,也可以通过内联的每个文件编译指示工作。要了解更多信息,请参阅他们的 tsconfig 参考页面:
推荐阅读
- sql - 如何从 Tableau 中的筛选器中排除值?
- symfony - Symfony 断言回调在管理类中不起作用
- python - 有没有办法在 python 中输入小写字母?
- python - 你如何区分python中的进程和线程?
- javascript - 如何编辑我的幻灯片以使其具有响应性?
- powerapps - PowerApps“无日期时间”控制小时偏移
- html - 如何在 Markdown 中编码并正确转换为 HTML?
- node.js - max-age、min-fresh 和 max-stale HTTP 标头如何从使用 Express js 作为服务器的客户端(请求标头)工作?
- python-3.x - FileNotFoundError:[Errno 2] 没有这样的文件或目录:'/Users/x/anaconda3/lib/python3.6/site-packages/numpy-1.19.4.dist-info/RECORD'
- python - 如何识别评论文本的情绪与评分不匹配的评论