react项目添加typescript类型定义文件 .d.ts
<p>最近用react + antd mobile + typescript开发项目,其中使用了rc-form这个包,可惜没有typescript版本,导致找不到类型定义。</p><p>一起来重温一下这个经典的错误。</p>
<div class="cnblogs_code">
<pre>D:/Code/test/src/pages/me/register/RegisterTest.tsx
TypeScript error <span style="color: rgba(0, 0, 255, 1)">in</span> D:/Code/test/src/pages/me/register/RegisterTest.tsx(5,28):
File 'D:/Code/test/src/rc-form.d.ts'<span style="color: rgba(0, 0, 0, 1)"> is not a module.TS2306
</span>3 | import BrowserHistory from '../../../router/BrowserHistory'<span style="color: rgba(0, 0, 0, 1)">;
</span>4 | import { AppUrls } from '../../../http/AppUrls'<span style="color: rgba(0, 0, 0, 1)">;
</span>> 5 | import { createForm } from "rc-form"<span style="color: rgba(0, 0, 0, 1)">;
</span>| ^
6 | import MyToast from '../../../componets/MyToast'<span style="color: rgba(0, 0, 0, 1)">;
</span>7 |
8 | export <span style="color: rgba(0, 0, 255, 1)">function</span> RegisterTest(props: any) {</pre>
</div>
<p>解决办法:</p>
<p>在网上查了很久,那些修改typeRoots的方法根本不好使,太坑人了。下面的方法绝对好用!</p>
<p>1. 修改项目根目录下的tsconfig.json文件,添加baseUrl选项,由于项目是用create-react-app创建的,这个值只能是src或者node_modules,推荐用src。</p>
<p><img src="https://img2020.cnblogs.com/blog/64257/202007/64257-20200707084423464-1345912747.png" alt="" loading="lazy"></p>
<p>2. 编写类型定义文件rc-form.d.ts,内容如下:</p>
<div class="cnblogs_code">
<pre>declare module 'rc-form'<span style="color: rgba(0, 0, 0, 1)"> {
export const createForm: Function;
}</span></pre>
</div>
<p>3. 将rc-form.d.ts放入项目的src目录下。</p>
<p>4. 在需要使用rc-form模块的文件里直接导入即可。</p>
<div class="cnblogs_code">
<pre>import { createForm } from "rc-form";</pre>
</div>
<p>===</p>
<p>最新方法:</p>
<p>tsconfig这样配置,注意typeRoots要放到compilerOptions结点下面。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
</span>"compilerOptions"<span style="color: rgba(0, 0, 0, 1)">: {
</span>"target": "es6"<span style="color: rgba(0, 0, 0, 1)">,
</span>"lib"<span style="color: rgba(0, 0, 0, 1)">: [
</span>"dom"<span style="color: rgba(0, 0, 0, 1)">,
</span>"dom.iterable"<span style="color: rgba(0, 0, 0, 1)">,
</span>"esnext"<span style="color: rgba(0, 0, 0, 1)">,
</span>"es2015"<span style="color: rgba(0, 0, 0, 1)">
],
</span>"allowJs": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"skipLibCheck": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"esModuleInterop": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"allowSyntheticDefaultImports": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"strict": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"forceConsistentCasingInFileNames": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"module": "esnext"<span style="color: rgba(0, 0, 0, 1)">,
</span>"moduleResolution": "node"<span style="color: rgba(0, 0, 0, 1)">,
</span>"resolveJsonModule": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"isolatedModules": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"noEmit": <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
</span>"jsx": "react"<span style="color: rgba(0, 0, 0, 1)">,
</span>"baseUrl": "src"<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(255, 0, 0, 1)"><strong>"typeRoots": [
"src/custom_types",
"node_modules/@types"</strong></span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"><strong>
]</strong></span>
},
</span>"include"<span style="color: rgba(0, 0, 0, 1)">: [
</span>"src"<span style="color: rgba(0, 0, 0, 1)">
]
}</span></pre>
</div>
<p>2. src目录下新建一个文件夹custom_types,放入类型定义文件,如下。</p>
<p><img src="https://img2020.cnblogs.com/blog/64257/202008/64257-20200810172613085-1147810853.png" alt="" loading="lazy"></p>
<p>===</p>
</div>
<div id="MySignature" role="contentinfo">
<div id="ZddSignature">
<div>作者:zdd</div>
<div>出处:http://www.cnblogs.com/graphics/
<div>
<div>本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.</div>
</div>
</div>
</div><br><br>
来源:https://www.cnblogs.com/graphics/p/13258855.html
頁:
[1]