noFallthroughCasesInSwitch
文件中启用了tsconfig.json选项。function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}
switch(getRandomInt(3)) {
/* falls through */
/* fall through */
/* FALLTHROUGH */
case 1: /* falls through */ /* fall through */ /* FALLTHROUGH */ /* <----- Still getting an error here "Fallthrough case in switch. (7029)" */
/* falls through */
/* fall through */
/* FALLTHROUGH */
console.log(1);
/* falls through */
/* fall through */
/* FALLTHROUGH */
case 2:
console.log(2);
break;
}
这个错误也可以在这个链接中看到:链接。但是TS游乐场中有一个错误,所以您必须手动单击"TS Config“菜单,然后勾选noFallthroughCasesInSwitch
选项,以便打开它,否则,您将不会看到错误。
发布于 2021-11-04 21:49:18
我最终解决了这个问题:我在tsconfig.json文件中禁用了tsconfig.json选项并安装了ESLint。
TypeScript做的皮条很少,过去TSLint是一个互补的链接器,现在被ESLint取代了。
我个人的观点是,TypeScript不应该自己建议对代码进行任何不会导致构建过程失败的更改,并且应该使用第三方的linting工具,比如ESList。只做一些衬里-导致不完善的规则和问题,就像我上面的问题。
发布于 2021-07-04 09:01:41
三种选择:
1-使用@ts-ignore
来抑制错误
正如您所做的那样,我总是会包含一条明确的评论,包括它所指向的case
:
function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}
switch(getRandomInt(3)) {
// @ts-ignore
case 1:
console.log(1);
// FALLS THROUGH to 2
case 2:
console.log(2);
break;
}
2-使用@ts-expect-error
(TypeScript 3.9+)
或者使用TypeScript 3.9,您可以使用@ts-expect-error
,以便如果有人编辑代码(或配置)以消除错误,TypeScript会警告您:
function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}
switch(getRandomInt(3)) {
// @ts-expect-error
case 1:
console.log(1);
// FALLS THROUGH to 2
case 2:
console.log(2);
break;
}
3-不要掉下去
或者,堆叠标签以使case 1
标签为空(它仍然会掉下来,但类型记录的noFallthroughCasesInSwitch
只会由掉穿的非空的大小写标签触发,而不是由空的标签堆叠而非空的标签触发):
function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}
const n = getRandomInt(3);
switch(n) {
case 1:
case 2:
if (n === 1) {
console.log(1);
}
console.log(2);
break;
}
https://stackoverflow.com/questions/68243059
复制相似问题