当前位置:网站首页>[typescript] cannot redeclare block range variables
[typescript] cannot redeclare block range variables
2022-06-30 05:12:00 【Catci】
List of articles
background
When using TypeScript + TSlint + Babel + Jest When setting up a development environment , In the development process, it is occasionally IDE Tips 「 Cannot redeclare a block range variable 」, This leads to compilation errors , The error reporting diagram is as follows :
Relevant development environment configurations are as follows :
- typescript: ^3.5.3
- tslint: ^5.19.0
- babel: ^7.0.0
- jest: ^24.9.0
- ts-jest: ^24.0.2
Solution
The reason tslint This error will be prompted , Because in Commonjs In the norm , Didn't like ESModule Capable of forming closures 「 modular 」 Concept , All modules are thrown to the global by default when they are referenced , So when a module is declared again ,TypeScript You will think that you have declared the same variable twice, and then throw it wrong .
For this question , The simplest solution is to add a line of code at the bottom of the error file :export {}. This line of code will 「 cheating 」tslint Make it think that the current file is a ESModule modular , Therefore, there is no possibility of repeated declaration of variables . When using this method , Remember to configure your tsconfig.json file :
{
"include": ["src", "demo"],
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"esModuleInterop": true, // important!
"target": "esnext",
"strict": true,
"outDir": "app",
"declaration": true,
"sourceMap": true
}
}
among esMoudleInterop This configuration allows... To appear in the file export keyword .
Another question
When you think everything is all right , You will find that the second problem has surfaced again : You cannot execute compiled JavaScript Code ! Yes , because babel Although it can help you translate successfully TypeScript Code , But it doesn't help you get rid of , you hack Upper export keyword , therefore node An error will be reported because the keyword is not recognized .
At this point you have two choices :
Delete export keyword , Endure annoying IDE Tips , To give way by force babel compile ;
Fortunately, , It really works , because tslint Just one. “lint”, It is only responsible for reminding you where there is a problem , You can force it to be ignored . however , When you use Jest coordination TypeScript This will not work when testing , because Jest Will be able to tslint The mistakes found are told to you as unforgivable mistakes , It means , You can't happily test your code , Of course, you can also choose the second solution :
Write a babel plug-in unit , Give Way babel Remove during translation export keyword ;
So your node The translated code can be recognized , Yours Jest No longer complain , The best of both worlds ! However , Do you really want to write a plug-in for this ?
If your first reaction is as big as mine 「NO!!!」, You should keep looking down , In fact, we have a third plan :)
The ultimate solution
actually , There is already one babel The plug-in can meet our needs :@babel/plugin-transform-modules-commonjs , This is what we have always dreamed of . As the plug-in name implies , It can be ESModule The module is converted to conform to Commonjs Standard code , And after my test , When you meet export {} Such an expression , The translation scheme is :「 Direct to ignore 」! This is exactly what we want !
That's it , In your babel.config.js Add this plug-in to ,TSlint No more complaints ,Jest Be able to test ,Node I won't yell at you “What the * export !!”, The whole world is clean .
Last , Let's share my complete set of related configurations , I hope you won't be bothered by this problem :
tsconfig.json
{
"include": ["src", "demo"],
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"esModuleInterop": true,
"target": "esnext",
"strict": true,
"outDir": "app",
"declaration": true,
"sourceMap": true
}
}
jest.config.js
module.exports = {
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testPathIgnorePatterns: ['/node_moudles/', './src/utils/test.ts'],
}
babel.config.js
module.exports = {
presets: ['@babel/typescript'],
plugins: [
'@babel/plugin-transform-modules-commonjs',
'@babel/proposal-class-properties',
'@babel/proposal-object-rest-spread',
],
}
边栏推荐
- Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
- Preorder traversal of Li Kou 589:n fork tree
- Revit secondary development - use panel function without opening the project
- Unity mobile end sliding screen rotation
- Chapter 7 vertex processing and drawing commands of OpenGL super classic (7th Edition)
- Unity3d Google Earth
- 炒美原油的国际交易平台如何能保障资金安全呢?
- Unity application class and data file path
- Set a plane to camera viewport
- Unity Logitech steering wheel access
猜你喜欢

力扣209. 长度最小的子数组

Procedural animation -- inverse kinematics of tentacles

QT connecting external libraries

Unity packaging failure solution

MinGW-w64下载文件失败the file has been downloaded incorrectly!

Parkour demo

【 VCS + Verdi joint simulation】 ~ Taking Counter as an Example

pytorch中常用损失函数总结

Network communication problem locating steps

One command to run rancher
随机推荐
[notes] unity Scrollview button page turning
How to install win7 on AMD Ruilong CPU A320 series motherboard
Unity scroll view element drag and drop to automatically adsorb centering and card effect
力扣209. 长度最小的子数组
Sourcetree usage
Installation and getting started with pytoch
Unity + hololens2 performance test
Force buckle 59 Spiral matrix II
Revit二次開發---未打開項目使用面板功能
Pit of smoothstep node in shadergraph
Chapter 12 pipeline monitoring of OpenGL super classic (version 7)
Unity mobile end sliding screen rotation
The difference between SVG and canvas
amd锐龙CPU A320系列主板如何安装win7
Four methods of unity ugui button binding events
Nestjs configures static resources, template engine, and post examples
Unity application class and data file path
Chapter 11 advanced data management of OpenGL super classic (version 7)
redis集群概念
Log writing specification