【eslint】Error: Unexpected lexical declaration in case block
项目代码迁移的时候遇到的,举个例子:
const getPersonInfo = (type: number) => {switch (type) {case 1:const name = '张三'; // Error: Unexpected lexical declaration in case block.eslintbreak;case 2:const name = '李四';console.log(name); // Uncaught SyntaxError: Identifier 'name' has already been declaredbreak;default:break;}
};getPersonInfo(2);
可以看到,case 1 和case 2 中定义的字段,理论上应该是互不影响的,但由于作用域的提升,case1 会影响到 case 2 。
官方文档解释,let、const、function、class直接放在case或default下使用都是不正确的,必须使用花括号。
const getPersonInfo = (type: number) => {switch (type) {case 1: {const name = '张三';break;}case 2: {const name = '李四';console.log(name);break;}default:break;}
};getPersonInfo(2);
下面是官方文档上的示例:(不正确的写法就是去掉花括号,这里就不贴出来了)
// Declarations outside switch-statements are valid
const a = 0;switch (foo) {// The following case clauses are wrapped into blocks using bracketscase 1: {let x = 1;break;}case 2: {const y = 2;break;}case 3: {function f() {}break;}case 4:// Declarations using var without brackets are valid due to function-scope hoistingvar z = 4;break;default: {class C {}}
}
【eslint】Error: Unexpected lexical declaration in case block相关推荐
- 解决 Unexpected lexical declaration in case block 的问题
亲测可用,若有疑问请私信 使用新版的 es-lint 的时候扫描旧的项目,发现报了 Unexpected lexical declaration in case block(no-case-decla ...
- 解决 Unexpected lexical declaration in case block.报错
问题: 写代码的时候突然报了 Unexpected lexical declaration in case block. 的错误 描述: 今天在写代码的时候突然报错了Unexpected lexica ...
- JavaScript => 解决 Unexpected lexical declaration in case block 的问题
使用新版的 es-lint 的时候扫描旧的项目,发现报了 Unexpected lexical declaration in case block(no-case-declarations) 这么一个 ...
- Unexpected lexical declaration in case block.
报错现象: 改写一:在case外声明变量 改写二:用var声明: switch (row.redirectType) {// 跳转类型:1.网页链接;2.app内部跳转---课程;4.社团详情---- ...
- 【异常】Error: ERROR 1012 (42M03): Table undefined. (state=42M03,code=1012)
[异常]Error: ERROR 1012 (42M03): Table undefined. (state=42M03,code=1012) 参考文章: (1)[异常]Error: ERROR 10 ...
- 【MySQL】ERROR 1045 (28000): Access denied for user的解决方法
[MySQL]ERROR 1045 (28000): Access denied for user的解决方法 参考文章: (1)[MySQL]ERROR 1045 (28000): Access de ...
- 【笔记】Error while loading PyV8 binary: exit code 1解决方法
[笔记]Error while loading PyV8 binary: exit code 1解决方法 参考文章: (1)[笔记]Error while loading PyV8 binary: e ...
- QT5对话框的中文字符串【乱码】 (error: C2001: 常量中有换行符)
问题解决:对话框的中文字符串[乱码] (error: C2001: 常量中有换行符) 假设编译环境:QT5+vs2015 项目配置: 默认编码:UTF-8 UTF-8 BOM: 目前存在了则保留 如果 ...
- 【Koa】Error: Cannot find module ‘koa-router‘
[Koa]Error: Cannot find module 'koa-router' 报错如下 解决办法 执行以下命令 安装koa-router 即可解决此问题 npm install koa-ro ...
最新文章
- c++ mysql 取出数据,c++从数据库的表中读取数据
- generator 和 yield的使用
- java ibatis 获取执行的sql_小程序官宣+JAVA 三大框架基础面试题
- mongodb服务部署
- SAP License:SAP凭证编号中跳号问题处理
- Leetcode每日一题:112.path-sum(路经总和)
- 宾得k5ii_K5--K5II---K5IIs,该选哪个?(喜欢宾得机的摄友)各抒己见。
- python opencv 摄像头亮度_python 基于opencv操作摄像头
- 中国黑客窃取《地狱之门》源代码公开叫卖
- windows常用快捷键(截图,录屏,放大镜,虚拟桌面,写字板,资源管理器快捷键)
- numpy 5 study task
- 客户管理系统CRM推荐
- 值得推荐的MAC软件下载软件的网站
- 使用Word制作文档封面
- 【PAC集成电机控制芯片】了解PAC集成电机控制芯片
- 1.5 Illustrator视图的放大与缩小
- 先验后验与贝叶斯定理
- SQL语言的四大功能
- react手脚架安装
- Github学生包申请流程