当前位置:网站首页>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进行增量编译这篇文章结合一下进行项目配置
边栏推荐
猜你喜欢
Talking about the current malpractice and future development
DAY ONE
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
Can online reload system software be used safely? Test use experience to share with you
Design a red envelope grabbing system
How rider uses nuget package offline
The best sister won the big factory offer of 8 test posts at one go, which made me very proud
How to implement Lua entry of API gateway
[communication] optimal power allocation in the uplink of two-layer wireless femtocell network with matlab code
Asset security issues or constraints on the development of the encryption industry, risk control + compliance has become the key to breaking the platform
随机推荐
The intranet penetrates the zerotier extranet (mobile phone, computer, etc.) to access intranet devices (raspberry pie, NAS, computer, etc.)
Talking about the current malpractice and future development
DevOps可以帮助减少技术债务的十种方式
leetcode:236. 二叉树的最近公共祖先
B 站弹幕 protobuf 协议还原分析
DAY FIVE
Wasserstein slim gain with gradient poverty (wsgain-gp) introduction and code implementation -- missing data filling based on generated countermeasure network
1000字精选 —— 接口测试基础
MATLIB reads data from excel table and draws function image
Detailed explanation of regular expression (regexp) in MySQL
Can online reload system software be used safely? Test use experience to share with you
Please help xampp to do sqlilab is a black
在docker中快速使用各个版本的PostgreSQL数据库
Master binary tree in one article
Gradle知識概括
ArrayExpress数据库里的细胞只有两个txt是不是只能根据Line到ENA下载测序跑矩阵?
JDBC programming of MySQL database
Daily question brushing record (XV)
PostgreSQL高可用之repmgr(1主2从+1witness)+Pgpool-II实现主从切换+读写分离
Leetcode problem solving - 889 Construct binary tree according to preorder and postorder traversal