项目代码迁移的时候遇到的,举个例子:

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相关推荐

  1. 解决 Unexpected lexical declaration in case block 的问题

    亲测可用,若有疑问请私信 使用新版的 es-lint 的时候扫描旧的项目,发现报了 Unexpected lexical declaration in case block(no-case-decla ...

  2. 解决 Unexpected lexical declaration in case block.报错

    问题: 写代码的时候突然报了 Unexpected lexical declaration in case block. 的错误 描述: 今天在写代码的时候突然报错了Unexpected lexica ...

  3. JavaScript => 解决 Unexpected lexical declaration in case block 的问题

    使用新版的 es-lint 的时候扫描旧的项目,发现报了 Unexpected lexical declaration in case block(no-case-declarations) 这么一个 ...

  4. Unexpected lexical declaration in case block.

    报错现象: 改写一:在case外声明变量 改写二:用var声明: switch (row.redirectType) {// 跳转类型:1.网页链接;2.app内部跳转---课程;4.社团详情---- ...

  5. 【异常】Error: ERROR 1012 (42M03): Table undefined. (state=42M03,code=1012)

    [异常]Error: ERROR 1012 (42M03): Table undefined. (state=42M03,code=1012) 参考文章: (1)[异常]Error: ERROR 10 ...

  6. 【MySQL】ERROR 1045 (28000): Access denied for user的解决方法

    [MySQL]ERROR 1045 (28000): Access denied for user的解决方法 参考文章: (1)[MySQL]ERROR 1045 (28000): Access de ...

  7. 【笔记】Error while loading PyV8 binary: exit code 1解决方法

    [笔记]Error while loading PyV8 binary: exit code 1解决方法 参考文章: (1)[笔记]Error while loading PyV8 binary: e ...

  8. QT5对话框的中文字符串【乱码】 (error: C2001: 常量中有换行符)

    问题解决:对话框的中文字符串[乱码] (error: C2001: 常量中有换行符) 假设编译环境:QT5+vs2015 项目配置: 默认编码:UTF-8 UTF-8 BOM: 目前存在了则保留 如果 ...

  9. 【Koa】Error: Cannot find module ‘koa-router‘

    [Koa]Error: Cannot find module 'koa-router' 报错如下 解决办法 执行以下命令 安装koa-router 即可解决此问题 npm install koa-ro ...

最新文章

  1. c++ mysql 取出数据,c++从数据库的表中读取数据
  2. generator 和 yield的使用
  3. java ibatis 获取执行的sql_小程序官宣+JAVA 三大框架基础面试题
  4. mongodb服务部署
  5. SAP License:SAP凭证编号中跳号问题处理
  6. Leetcode每日一题:112.path-sum(路经总和)
  7. 宾得k5ii_K5--K5II---K5IIs,该选哪个?(喜欢宾得机的摄友)各抒己见。
  8. python opencv 摄像头亮度_python 基于opencv操作摄像头
  9. 中国黑客窃取《地狱之门》源代码公开叫卖
  10. windows常用快捷键(截图,录屏,放大镜,虚拟桌面,写字板,资源管理器快捷键)
  11. numpy 5 study task
  12. 客户管理系统CRM推荐
  13. 值得推荐的MAC软件下载软件的网站
  14. 使用Word制作文档封面
  15. 【PAC集成电机控制芯片】了解PAC集成电机控制芯片
  16. 1.5 Illustrator视图的放大与缩小
  17. 先验后验与贝叶斯定理
  18. SQL语言的四大功能
  19. react手脚架安装
  20. Github学生包申请流程

热门文章

  1. JNI Local Reference Changes in ICS
  2. Java Set类简介说明
  3. MCU-DFLASH-ECC错误
  4. java 怎么执行数据库命令_java 调用命令 备份mysql数据库
  5. CSDN请你吃汤圆啦!猿宵节万元礼包限时抢
  6. python3周末班2期第3天
  7. 黑客攻防学习笔记 入门篇
  8. 第 10 章 容器:数组(下)
  9. 杨建允:王老吉借乳制品发力新运营,看王老吉吉如意纯牛奶破圈操作!
  10. 如何软件破解它的原理解析如何去操作(第一集)