当前位置:网站首页>TypeScript中使用类型别名
TypeScript中使用类型别名
2022-07-06 16:30:00 【用户6256742】
在很多打包工具或者使用cli创建的项目中都会提供类型别名,例如Vue-cli中使用@可以代表绝对路径src。
但是在使用TypeScript开发Node.js项目中却没有这个选择,当然我们可以在tsconfig.json中设置path参数,但是这个只是路径不报错和有利于路径提示,在ts-node运行时还是会报错,社区中提供了一个叫typescript-paths的插件来解决问题,但是这个插件对增量编译非常不友好(ts在项目大了之后全量编译随便改一点就要等2分钟),对此我们可以使用插件module-alias来解决。
1.首先我们依然是在tsconfig.json中设置ts的路径别名,这是是可以让ts不报错并且有良好的路径提示。
"paths": {
"@/*": ["src/*"]
},Copy
JSON
2.安装插件module-alias,这个非常简单yarn add module-alias或者npm intall module-alias
3.在入口文件的顶部配置路径别名(可以在package.json里面但是增量编译的话需要额外复制一个文件)
// 变量别名
import moduleAlias from "module-alias";
moduleAlias.addAlias("@", __dirname);Copy
TypeScript
整个配置可以配置多个别名,同时可以编写回调函数。如果你是使用了TS进行项目开发可以结合TS+Node.js进行增量编译这篇文章结合一下进行项目配置
边栏推荐
- 士大夫哈哈哈
- If the request URL contains jsessionid, the solution
- 基础图表解读“东方甄选”爆火出圈数据
- 1000 words selected - interface test basis
- Cas d'essai fonctionnel universel de l'application
- The programmer refused the offer because of low salary, HR became angry and netizens exploded
- matplotlib画柱状图并添加数值到图中
- Eureka Client启动后就关闭 Unregistering application xxx with eureka with status DOWN
- 若依请求url中带有jsessionid的解决办法
- MATLIB reads data from excel table and draws function image
猜你喜欢

What should I do if the USB flash disk data is formatted and how can I recover the formatted USB flash disk data?

How to implement Lua entry of API gateway
Example code of MySQL split string as query condition
![[OFDM communication] OFDM system signal detection based on deep learning with matlab code](/img/a5/624860f6bd9be03ac8c1f61839fea2.png)
[OFDM communication] OFDM system signal detection based on deep learning with matlab code

Gradle知识概括

【自动化测试框架】关于unittest你需要知道的事

app通用功能測試用例

Interface joint debugging test script optimization v4.0

Rider离线使用Nuget包的方法

DAY FIVE
随机推荐
The programmer refused the offer because of low salary, HR became angry and netizens exploded
本地部署 zeppelin 0.10.1
2022 latest blind box mall complete open source operation source code / docking visa free payment interface / building tutorial
Today, I met a senior test developer from Tencent and saw the ceiling of the foundation
Close unregistering application XXX with Eureka with status down after Eureka client starts
基于SSM框架实现的房屋租赁管理系统
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
PostgreSQL高可用之repmgr(1主2从+1witness)+Pgpool-II实现主从切换+读写分离
leetcode:236. 二叉树的最近公共祖先
Wind chime card issuing network source code latest version - commercially available
基础图表解读“东方甄选”爆火出圈数据
内网穿透zerotier 外网(手机、电脑等)访问内网设备(树莓派、NAS、电脑等)
Wasserstein Slim GAIN with Gradient Penalty(WSGAIN-GP)介绍及代码实现——基于生成对抗网络的缺失数据填补
Matplotlib draws a histogram and adds values to the graph
Who said that new consumer brands collapsed? Someone behind me won
若依请求url中带有jsessionid的解决办法
JDBC programming of MySQL database
I've been laid off, and I'll lose money for everything. The days when I once made a monthly salary of 20000 are not coming back
Basic chart interpretation of "Oriental selection" hot out of circle data
每年 2000 亿投资进入芯片领域,「中国芯」创投正蓬勃