当前位置:网站首页>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进行增量编译这篇文章结合一下进行项目配置
边栏推荐
- 三句话简要介绍子网掩码
- Wasserstein Slim GAIN with Gradient Penalty(WSGAIN-GP)介绍及代码实现——基于生成对抗网络的缺失数据填补
- Wu Enda 2022 machine learning course evaluation is coming!
- leetcode:236. The nearest common ancestor of binary tree
- Building lease management system based on SSM framework
- Every year, 200 billion yuan is invested in the chip field, and "China chip" venture capital is booming
- Hydrogen future industry accelerates | the registration channel of 2022 hydrogen energy specialty special new entrepreneurship competition is opened!
- Competition between public and private chains in data privacy and throughput
- One minute to learn how to install the system, win7 XP, win10 and win11 become very simple
- Common modification commands of Oracle for tables
猜你喜欢
Do you still have to rely on Simba to shout for a new business that is Kwai?
1000字精选 —— 接口测试基础
2022 latest blind box mall complete open source operation source code / docking visa free payment interface / building tutorial
Gradle知识概括
吴恩达2022机器学习课程评测来了!
Interface joint debugging test script optimization v4.0
How to implement Lua entry of API gateway
服务器SMP、NUMA、MPP体系学习笔记。
Zero code and high return. How to use 40 sets of templates to meet 95% of the reporting needs in the work
Close unregistering application XXX with Eureka with status down after Eureka client starts
随机推荐
DAY ONE
[system analyst's road] Chapter 7 double disk system design (service-oriented development method)
PDF文档签名指南
在Docker中分分钟拥有Oracle EMCC 13.5环境
STM32通过串口进入和唤醒停止模式
The largest single investment in the history of Dachen was IPO today
Can async i/o be implemented by UDF operator and then called by SQL API? At present, it seems that only datastre can be seen
为什么完全背包要用顺序遍历?简要解释一下
PostgreSQL使用Pgpool-II实现读写分离+负载均衡
Competition between public and private chains in data privacy and throughput
How does win11 restore the traditional right-click menu? Win11 right click to change back to traditional mode
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
基于jsp+servlet+mysql框架的旅游管理系统【源码+数据库+报告】
How rider uses nuget package offline
本地部署 zeppelin 0.10.1
The tutorial of computer reinstallation win10 system is simple and easy to understand. It can be reinstalled directly without U disk
若依请求url中带有jsessionid的解决办法
吴恩达2022机器学习课程评测来了!
DAY FIVE
《数字经济全景白皮书》保险数字化篇 重磅发布