reactjs - 当我尝试从库中导入 TypeScript 接口时出现 Webpack“未找到模块”错误?
问题描述
我有一个 React 项目,我正在将它转换为 TypeScript,所以新组件是 TypeScript,而旧组件是 JavaScript。我还通过它的 React 组件介绍了 FontAwesome。
一切正常,直到我导入一个fontawesome-common-types
导致编译错误的接口。
这一直有效,直到我取消注释第一行:
// import { IconDefinition } from '@fortawesome/fontawesome-common-types';
import * as React from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faAngleRight } from '@fortawesome/pro-solid-svg-icons';
export type Props = {
links: {
text: string;
icon: any; // This is what I need IconDefinition for
url: string;
}[];
};
const MyComponent: React.FC<Props> = ({ links }) => {
return (
<ul>
{
links.map((link,index)=>(
<li key={index}>
<a href={link.url}>
{link.text}
<FontAwesomeIcon icon={link.icon} />
</a>
</li>
))
}
</ul>
);
};
此时我收到此错误:
./src/components/MyComponent/MyComponent.tsx 中的错误找不到模块:错误:无法解析“/Users/MYNAME/projects/PROJECT/src/components/MyComponent”中的“@fortawesome/fontawesome-common-types”
在 tsconfig.json 中:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"exclude": [
"node_modules"
],
"include": [
"**/*.ts",
"**/*.tsx"
]
}
解决方案
推荐阅读
- c++ - Is there a way to pass lambda expression as a predicate template argument?
- java - OpenClover - 开始使用 AspectJ
- r - Running mean with condition in R?
- javascript - 将数组作为输入传递给组件/函数
- r - Julia 的包中是否有 rep() 函数?
- c# - 使用 C# 检索远程计算机的进程信息
- python - 后续 rolling_apply 已弃用
- node.js - 错误:存根无法调用 arg,因为它尚未被调用
- java - 创建组合两个但由一个统治的可观察对象
- ansible - 在字典列表上使用 Ansible with_dict 进行迭代